Using a Chrono for Timer

squaremation

Active Member
Licensed User
Longtime User
Im trying to make a game and use the chronometer as a timer for each level. Not sure what I'm missing but I'm not sure how to check to chronometer's time.

This is the last code I tried
B4X:
 Sub Chrono_Tick
   If Not(Resuming) Then
      secs = Chrono.Text.SubString(Chrono.Text.Length - 1)
          If secs = "30" Then     'supposed to stop timer then check score but just keeps running
         Level_Define
               End If   
        End If
   Level_Define
End Sub

Sub Level_Define
   Dim x As Int    
   Dim iScore As Int
   Dim iLevelAdvance() As Int
   iLevelAdvance = Array As Int ("20","40","60","80") 'array of points needed for each level
   iScore = lblCurrentScore.Text            'checks the score
   x = iLevelAdvance(Main.Level)           'sets level to match points needed
   
   If iScore >= x Then              'suppose to check if user's score is enough for next level 
         Main.Level = Main.Level  + 1   'changes level   
         StartActivity(LevelAdvance)   'activity that shows score and next level
      Else
         Player_Lose                          'game over
      End If
End Sub

Not sure if it's the Chron code or the Level_Define routine thats not working.
:BangHead:
 

squaremation

Active Member
Licensed User
Longtime User
Resolved(sort of), sense I guess there is no way to "check" the time in a Chronometer or have Tick (in a Chronometer) run a sub routine.

Changed code so I have a timer that start when the Chronometer is initialized.

B4X:
Sub Globals
    Dim levelTimer As Timer
   Dim Ticks As Int 
End sub


B4X:
Sub Activity_Create(FirstTime As Boolean)
   'timer
   Chrono.Initialize("Chrono")
   ChronoBase = Chrono.GetElapsedRealTime
   Chrono.BaseTime = ChronoBase
   Chrono.Start
   
   levelTimer.Initialize("levelTimer", 1000)
   levelTimer.Enabled = True
End sub

B4X:
Sub levelTimer_Tick
   If Ticks < 30 Then
      Ticks = Ticks + 1
   Else
   'StartActivity(LevelAdvance)
   Chrono.Stop
   
   Level_Define
   
   End If
End Sub

Sub Level_Define
   Dim x As Int
   Dim iScore As Int
   Dim iLevelAdvance() As Int
   iLevelAdvance = Array As Int ("20","40","60","80")
   iScore = lblCurrentScore.Text
   x = iLevelAdvance(Main.Level)
   
   If iScore >= x Then 
         Main.Level = Main.Level  + 1         
         StartActivity(LevelAdvance)
      Else
         Player_Lose
      End If
End Sub

Not sure if this the most efficient, but works, now I'll just look for a way to reset Chronometer, sense when StartActivity(LevelAdvance) is called and it only pauses the Activity w/the Chronometer, need it to reset to 0. Any assit or suggestions appreciated.:eek:
 
Upvote 0
Top