Android Question [SOLVED]Implementing Timer in LibGdx

abhishek007p

Active Member
Licensed User
Longtime User
Hi! Anyone can help me on how will i implement a timer in LibGdx?..

Im creating a countdown timer that i will use on the game im creating.

B4X:
Sub Globals
Dim COUNTDOWN_TIMER As Int = 60 'equiv. to 60 seconds
End Sub

Then on every timer tick (per second), subtract 1 to COUNTDOWN_TIMER

thanks,
---------------------------------------
EDIT: Found the Solution
---------------------------------------

B4X:
#Event: Tick()

'Class module
Private Sub Class_Globals
    Public Interval As Float = 1            'Interval in seconds
    Private DELTA_TIME_COUNTER As Float = 0
    Private ParentActivity As Object
    Private EventHandle As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
'Example: Initialize(Me, "Timer", 1)
Public Sub Initialize(mParent As Object, mEventHandle As String, mInterval As Float)
    ParentActivity = mParent
    Interval = mInterval
    EventHandle = mEventHandle
End Sub

Sub UpdateTimer(DeltaTime As Float)
    DELTA_TIME_COUNTER = DELTA_TIME_COUNTER + DeltaTime
    If DELTA_TIME_COUNTER >= Interval Then
        DELTA_TIME_COUNTER = 0
        If SubExists(ParentActivity, EventHandle & "_Tick") Then
            CallSub(ParentActivity, EventHandle & "_Tick")
        End If
    End If
End Sub

Sub FormatToTime(TimeInSeconds As Float) As String
    Dim s1 As String = NumberFormat(Floor(TimeInSeconds / 60), 2, 0)
    Dim s2 As String = NumberFormat(TimeInSeconds Mod 60, 2, 0)
    Return s1 & ":" & s2
End Sub
 
Last edited:
Top