I'm really sorry I have to ask this (About timers)

It seems like the easiest thingy… but I can’t figure it out
I want a timer to start counting up when the game begins 00:00 (seconds,hundredths)

Please help, I’m scratching my hair here! :slight_smile:
[import]uid: 10657 topic_id: 3862 reply_id: 303862[/import]

startTime=system.getTimer
lapsedTime=system.getTimer-startTime [import]uid: 7911 topic_id: 3862 reply_id: 11806[/import]

Is it ‘sensible’ to call system.getTimer in an “enterFrame” function, i.e.

[blockcode]
Runtime:addEventListener( “enterFrame”, pmain )
function pmain()
lapsedTime=system.getTimer-startTime
end
[/blockcode]

I’m still trying to work out the best way to setup a game loop to spawn certain events, is using a function like this and a timer the best way to do it?

I really wish there were more indepth ‘real world’ examples to examine :frowning:

[import]uid: 9371 topic_id: 3862 reply_id: 11811[/import]

You could setup a variable lapedTime =0.00 outside of pmain then inside pmain lapsedTime=lapsedTime+0.01 but may not be accurate [import]uid: 7911 topic_id: 3862 reply_id: 11812[/import]

[lua]local textObject = display.newText( “0”, 140, 25, “Courier”, 20 )
textObject:setTextColor( 255,215,175 )

startTime=system.getTimer()

local function timeCheck( event )

lapsedTime=system.getTimer()-startTime
textObject.text = lapsedTime / 1000

end

Runtime:addEventListener( “enterFrame”, timeCheck )[/lua]
This gets pretty much what I want but it jitters in width and I only want two decimals. Is there some code I can add to fix this? [import]uid: 10657 topic_id: 3862 reply_id: 11813[/import]

Hello again!

Here is what I ended up using if anyone else needs it:

[lua] local x = os.clock()
local s = 0

local function timeCheck( event )
for i=1,1000000 do s = s + i end

textObject.text = string.format(“elapsed time: %.2f\n”, os.clock() - x )
end
[lua] [import]uid: 10657 topic_id: 3862 reply_id: 11850[/import]

but what calls your timeCheck() [import]uid: 9371 topic_id: 3862 reply_id: 11851[/import]

This:
Runtime:addEventListener( “enterFrame”, timeCheck );

Now I just have to figure out how to get this bad boy into openfeint and we’re all set :slight_smile: [import]uid: 10657 topic_id: 3862 reply_id: 11852[/import]

The thing that concerns me is that this:

Runtime:addEventListener( “enterFrame”, timeCheck );

will mean that timecheck gets calls every frame, then you have a loop which has to complete before the next frame can happen no? Won’t this slow down the app horribly? [import]uid: 9371 topic_id: 3862 reply_id: 11853[/import]

I don’t know, how would you otherwise update the time? [import]uid: 10657 topic_id: 3862 reply_id: 11856[/import]

I’m having some questions on creating a timer clock for my game - the player must be fast in my game, and the countdown starts from 0 to whatever time the level takes.

I’ll be using your thread, rickwhy, because I think we share the same questions and this will keep the forum organized :slight_smile:

So:
1- Shall I use the os.time parameters or the system.timer?
I’ve been messing with the system.timer so far, but I find it very imprecise. For example a function like this…
[lua]function elapsedTime (event)
theTime = system.getTimer() - startTime
debugText.text = "Time: " … math.floor(theTime / 1000)
end

startTime = system.getTimer()
timer.performWithDelay( 1000, elapsedTime, 0)[/lua]
…is fine, but sometimes the clock will skip a second (because the var theTime is not increasing in 1000’s. I wanted this to be as rare as possible!
2- What’s the best way to include digits before the a one-digit second?
Here’s the thing… I wanted the clock to be based on a 00:00 base. So, for example, 3 minutes and 8 seconds would be displayed as “03:08” and not “3:8”. I still couldn’t do the “zero insertion” in the string when the theTime division result has only one algarism.

Thanks guys!

EDIT
Check my function. Using the perform with delay is better than checking the timer each frame! :wink:
[import]uid: 11130 topic_id: 3862 reply_id: 11862[/import]

haha i tried my app on the iphone. totally useless :slight_smile: gonna try your code! [import]uid: 10657 topic_id: 3862 reply_id: 11874[/import]

Alright so perform with delay is the way to go…

But I still want to have SECONDS : HUNDRETHS … is that even possible? [import]uid: 10657 topic_id: 3862 reply_id: 11881[/import]

to get a leading “0” add a 100 to your number and then take the last 2 characters

[lua]local num=9
print(string.sub(100+num,2))[/lua]
[import]uid: 6645 topic_id: 3862 reply_id: 11880[/import]

this one solves all the problems with timers:

http://developer.anscamobile.com/code/beebegames-class

:slight_smile: [import]uid: 11382 topic_id: 3862 reply_id: 20573[/import]