Android Question I want to use timer on the game i'm making. Having a hard time figuring it out.

xicode

New Member
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim tmr As Timer
Dim remainingSeconds As Int
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Private ImageView1 As ImageView
Private cb1smg1 As CheckBox
Private cb2smg1 As CheckBox
Private cb3smg1 As CheckBox
Private cb4smg1 As CheckBox
Dim timerTextView As EditText

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("SM_GamePlay1")

' Initialize the tmr timer
tmr.Initialize("tmr", 1000) 'Tick every second (1000 milliseconds)
tmr.Enabled = True

remainingSeconds = 15

' Attempt to get the EditText with ID "timerTextView"
timerTextView = Activity.GetView("timerTextView")
If timerTextView = Null Then
Log("Error: timerTextView not found in layout!")
End If
End Sub

Sub tmr_Tick
'Update the TextView with the remaining time
timerTextView.Text = "Timer: " & remainingSeconds & " seconds"

'Decrement remaining time
remainingSeconds = remainingSeconds - 1

'Stop the Timer when countdown reaches 0
If remainingSeconds = 0 Then
tmr.Enabled = False
Log("Timer finished!")
'Add any code to execute after timer finishes
End If
End Sub




Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub cb4smg1_CheckedChange(Checked As Boolean)

If Checked Then
cb4smg1.Color = Colors.Red
Else
cb4smg1.Color = 0XFF11DBB2
End If

End Sub

Private Sub cb3smg1_CheckedChange(Checked As Boolean)

If Checked Then
cb3smg1.Color = Colors.Red
Else
cb3smg1.Color = 0XFF11DBB2
End If

End Sub

Private Sub cb2smg1_CheckedChange(Checked As Boolean)

If Checked Then
cb2smg1.Color = Colors.Red
Else
cb2smg1.Color = 0XFF11DBB2
End If

End Sub

Private Sub cb1smg1_CheckedChange(Checked As Boolean)

If Checked Then
cb1smg1.Color = Colors.Red
Else
cb1smg1.Color = 0XFF11DBB2
End If

End Sub


Private Sub timerTextView_TextChanged (Old As String, New As String)

If remainingSeconds > 0 Then
timerTextView.Text = "Timer: " & remainingSeconds & " seconds"
Else
timerTextView.Text = "Timer: Time's up!"
End If
End Sub
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
1. Please use
B4X:
code here...
tags when posting code.
1715412923020.png


2. Upload a small project. Its easier for sometime run and debug

3. You did not provide enough information, We dont now where the hard time of figuring out is. Explain your issue in detail
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
There is a lot of unnecessary code in your example, not just involving the timer. Here is a simple program ...
B4X:
Sub btnStart_Click
    lblTimer.Text = "10 seconds"
    timeRemaining = 10                          ' Set the time period
    tmr.Enabled = True                            ' Start the timer
    btnStart.Enabled = False                    ' Stop the user restarting the timer
End Sub

Sub tmr_Tick
    timeRemaining = timeRemaining - 1  ' Count down
    If (timeRemaining > 0) Then                ' Timer has not expired
        lblTimer.Text = timeRemaining & " seconds"
    Else
        lblTimer.Text = "Time's up!"        ' Timer has expired
        tmr.Enabled = False                      ' Stop the timer
        btnStart.Enabled = True                ' Enable the start button
    End If
End Sub

Note that I have used a label rather than an edittext because you do not want to the User to edit the timer message. Here is a complete mini-project if you want to try experiments.
 

Attachments

  • Timer.zip
    9.3 KB · Views: 92
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
timerTextView = Activity.GetView("timerTextView")
1715504410767.png


the GetView method expects an integer and not a string. you cannot get the view by name like you are trying to do.
this is your first mistake

you second mistake is to post code not in code format </> and also a simple example would help to help you.
 
Upvote 0
Top