Curious Millisecs thing

BlitzMax Forums/BlitzMax Programming/Curious Millisecs thing

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:
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:


I ve read your post a bit fast so maybe I m missing something but I think the problem comes from this line:
in your UpdateTimer method you have:
FCurrentTime = MilliSecs() - FStartTime
when you resume you do FStartTime = Millisecs()
and thus when calling UpdateTimer, MilliSecs() - FStartTime is equal to 0
and your if statement (If FCurrentTime - FLastTickTime => FInterval Then) is false until you ve catched back the time the Timer has been alive...

(and just a small, very small remark, you can replace the following:
frame:+1
If frame > 9 Then frame = 0
by:
frame = (frame + 1) Mod 10
)