I've been messing around trying to create a timer that triggers at a specified interval and it appears to be working except that with the test code I am disabling the timer on left click and enabling on right click but it takes a good 2-3 seconds to re-enable unless I comment out the code that resets the FStartTime field to Millisecs()
EventTimer.bmx:
test code:
the image I'm using:
EventTimer.bmx:
Type TEventTimer Field FInterval : Int Field FTickCount : Int Field FLastTickTime : Int Field FCurrentTime : Int Field FStartTime : Int Field FOnTick() Field FEnabled : Int Rem CONSTRUCTOR Function create accepts an int value for the interval in milliseconds EndRem Function Create:TEventTimer(Interval:Int, Event()) Local tempTimer:TEventTimer = New TEventTimer tempTimer.FInterval = Interval tempTimer.FOnTick = Event Return tempTimer End Function Rem EndRem Method UpdateTimer() If Not FEnabled Then Return FCurrentTime = MilliSecs() - FStartTime 'check if the next tick time is here or if the tick event has not been triggered If FCurrentTime - FLastTickTime => FInterval Then FLastTickTime :+ FInterval 'ensure that no ticks are missed FOnTick() EndIf End Method Rem pass 1 to enable 0 to disable EndRem Method Enable(Enabled:Int) If (FEnabled = False) And (Enabled) Then FStartTime = MilliSecs() 'if I comment this line out it resumes instantly EndIf FEnabled = Enabled End Method Method SetInterval(Interval:Int) FInterval = Interval End Method End Type
test code:
Import "EventTimer.bmx" Graphics 800,600,0 Global ball:TImage = LoadAnimImage("ball_anim.png", 64,64,0,10, MASKEDIMAGE) Global t:TEventTimer = TEventTimer.Create(10, AnimateBall) Global frame = 0 t.Enable(1) While Not KeyDown(KEY_ESCAPE) t.UpdateTimer() DrawImage(ball, MouseX(), MouseY(), frame) If MouseDown(1) Then t.enable(0) If MouseDown(2) Then t.enable(1) Flip Cls Wend End Function AnimateBall() frame:+1 If frame > 9 Then frame = 0 End Function
the image I'm using: