I need to do a game timer countdown from 60 seconds. I’ve got some of the basic functions down that I think that I would use, like timer.performWithDelay, but for actually outputting the time to the display I’m not quite sure. I guess their would be several ways to go, like a text output filed of some sort, or a spritesheet with the numbers. Is their a recommended approach or perhaps an example of such a timer I could look at?
Thank you
[import]uid: 8139 topic_id: 3069 reply_id: 303069[/import]
Hi there,
You can use the System Timer, as follows. The RunTime listener just keep checking how much time has expired, every game cycle, then it executes upon your logic.
-- display timer text on screen
local textObject = display.newText( "0", 140, 25, "Courier", 20 )
textObject:setTextColor( 255,215,175 )
local function timeCheck( event )
textObject.text = system.getTimer() -- update timer display; you can manipulate its "accuracy" using math functions
if ( system.getTimer() \>= 60000 ) then --60000 is in milliseconds of course, so 60 real seconds
-- game over! (call some other function now!)
end
end
-- at the very end of your document:
Runtime:addEventListener( "enterFrame", timeCheck );
Hope this helps!
Brent
[import]uid: 9747 topic_id: 3069 reply_id: 9170[/import]
I’m getting a problem with this. The timer starts immediately even if Runtime:addListener("enterFrame", timeCheck) is being called from a button event so I had to find a workaround. Also, there seems to be no stopping it, Runtime:removeEventListener("enterFrame", timeCheck) just doesn’t work.
I need to be able to start and stop the timer during the beginning and end of each level.
It’s working for me, but I am using performWithDelay instead of a Runtime event listener. This seems to work great:
local gameTime=1
local timeCheck = {}
function timeCheck:timer( event )
gameTime=gameTime+1
textObject.text =gameTime
if ( gameTime > 59 ) then
timer.cancel(t2)
– other end of game functions here
end
end
Enterframe is probably not clever. You dont need to update the text field ever frame anyway, A performWithTimer is better. If you need very tight precision use get system timer and check it inside your perfromWithTimer thats called ever second. [import]uid: 8872 topic_id: 3069 reply_id: 43580[/import]
Hi
Do you know how to show timer with miliseconds ? I need 5 seconds with miliseconds.
This i what I have done :
local counter = 500
local function updateCount()
counter=counter-1
timeText:setText(counter)
if ((counter ) == 0) then
audio.play(popSound)
timer.cancel( clockTimer )
game = false
finalScreen()
end
end
clockTimer = timer.performWithDelay( 10, updateCount, counter )
But this is about 7 seconds real time ??? [import]uid: 13156 topic_id: 3069 reply_id: 43859[/import]
1 second is 1000 miliseconds… so I am wrong for sure.
Did someone made a counter with miliseconds in this format… 5:00 [import]uid: 13156 topic_id: 3069 reply_id: 43861[/import]