Countdown timer

BlitzMax Forums/BlitzMax Programming/Countdown timer

Gavin Beard(Posted 2008) [#1]
Hi all

i am trying to create a countdown timer, it is only to work in seconds, i've tried various methods such as timers, to grabbing the millisecs and using mod 1000, none of which seem to work, anyone able to help me out on this one, give me a brief over view of how to get a countdown working???

many thanks


GfK(Posted 2008) [#2]
Quick and dirty example.
Strict

Graphics 640,480

Global timer:ttimer = CreateTimer(1)
Global secsRemaining:Int = 10


While (Not KeyDown(key_escape)) And secsRemaining > 0
	Cls
	DrawText "Time Left: " + secsRemaining,10,10
	Flip
	secsRemaining:-WaitTimer(timer)

Wend

End

Note: WaitTimer() halts execution until it can return a non-zero value. If you don't want that to happen then you can have the timer tick more often than once a second (see CreateTimer(1)), or:
Strict

Graphics 640,480

Global timer:ttimer = CreateTimer(1)
Global secsRemaining:Int = 10


While (Not KeyDown(key_escape)) And secsRemaining > 0
	Cls
	DrawText "Time Left: " + secsRemaining,10,10
	DrawText MilliSecs(),10,30 'to prove its redrawing the screen
	Flip
	If timer.ticks() > 0
		secsRemaining:-timer.ticks()
		timer._ticks = 0
	EndIf
Wend

End



Gavin Beard(Posted 2008) [#3]
Legend

Close to what i had, but far enough off that yours actually works :-)

thanks Gfk

*edit, will this slow down a game, waiting for a timer to tick?
*thanks, you beat me to it, lol


klepto2(Posted 2008) [#4]
Hi, I'm using something like this in my apps:



The main advantage of this is you don't have to worry about the update.


Gavin Beard(Posted 2008) [#5]
Thats brilliant,

I had wrapped the first example into a working type, but had to call an update manually each frame, this works great.

thanks


klepto2(Posted 2008) [#6]
	Function DownHook:Object( id:Int , data:Object , context:Object )
		Local CD:TCountDown = TCountDown(context) 
		If CD <> Null Then
			CD.Update() 
		End If
		Return data
	End Function 


should be

	Function DownHook:Object( id:Int , data:Object , context:Object )
		Local CD:TCountDown = TCountDown(context) 
		If CD <> Null And TEvent(data).Source = TCountdown.Timer Then
			CD.Update() 
		End If
		Return data
	End Function 


Otherwise each emitted event will potentially count down the timers. But with this you make sure that only the TCountdown.Timer event will be handled.


ImaginaryHuman(Posted 2008) [#7]
Surely all you need to do is very simply:

1) Store the current Millisecs/1000 into a StartTime variable when you start the countdown

2) Calculate TimeElapsed=(Millisecs/1000)-StartTime

3) Calculate countdown with SecondsToWait-TimeElapsed