iOS Tutorial T3 - The Timer Tutorial

Mark Turney

Active Member
Licensed User
During the creation of Callosum, I used 7 different timer for everything from level transitions to the level countdown and overall count up timers.

Here's a simple summary of the steps to create and use a timer:

1) Declare the timer in Sub Process_Globals (Private timer1 as timer).
2) Initialize the timer with the time in ms you want for the "tick" in Private Sub Application_Start. In the case of a countdown timer, I set it to 1000, which is 1 second (timer1.Initialize("Timer1", 1000)).
3) Enable / Disable the timer when you want it to run / turn off respectively - this is done throughout the app as needed (timer1.enabled = true (or false for off)).
4) Whatever code is in the timer_tick subroutine runs only while the timer is enabled and at the interval of the tick ... in the case of the timer1 example, 1 sec (1000 ms).

Here's code for a level countdown timer:
B4X:
Sub Timer1_Tick                                                    'Game timer
   timeCnt = timeCnt - 1                                                    'Countdown 1s for each tick
   pcScore.Text = timeCnt                                                  'Display the countdown as a label
   If timeCnt = 0 Then                                                        'When countdown hits 0, turn off this and overall count up timers, plus some other stuff for my app ;-)
          phone1.Vibrate
       Msgbox2("Try", "", "L E V E L " & levelcnt & " not completed! Would you like to try again?", Array("Yes", "No"))
       'levelcnt = 0 'score = 0
       timeCnt = 240 - ((levelcnt-1) * 20)
       timer1.Enabled = False
       timer7.Enabled = False
       totScore.TextColor = Colors.RGB(100,0,0)
   End If
End Sub
Once the user proceeds to the next level, I set timer1.enabled = true, and overall timer text color to a dark green totScore.TextColor = Colors.RGB(0,100,0). Note the overall timer (7) is a dark red (100,0,0) in the code above while it is stopped.

Hope this helps!
 
Last edited:

Mark Turney

Active Member
Licensed User
You mean for instance where it say Msgbox2 ("Try ..... ?

Here is the code for the "Try" subroutine:
B4X:
Sub Try_click(ButtonText As String)                                'Try over subroutine
    If ButtonText = "Yes" Then
        score = levelcnt
        timeCnt = 240 - ((levelcnt-1) * 30)
        'phone1.Vibrate
        C = 3
        timer4.Enabled = True
        'rndValues
    Else 
        'tm.ToastMessageShow("Good luck next time!", True)
        timer1.Enabled = False
    End If
End Sub
Hope this helps!
 
Top