Game Timer

I’d like to make a timer for game play, then have it log to a leaderboard.

I can make get minutes and seconds to work, but not milliseconds.

Any help would be great

timer_help.jpg

[lua]

local function updateTime( event )

   gameText.counterTenths = gameText.counterTenths + 1

   gameText.minutes = math.floor( gameText.counterSeconds / 60 )

   gameText.seconds = math.floor( gameText.counterTenths / 100 )

   gameText.tenths = gameText.counterTenths % 100

     

   gameText.timeDisplay = string.format( “%02d:%02d:%02d”, gameText.minutes, gameText.seconds, gameText.tenths )

   gameText.clockText.text = gameText.timeDisplay

end

misc.counterTimer = timer.performWithDelay(1,updateTime, 0)

[/lua]

It’s not going to work either. Corona only updates the screen ever 1 / fps seconds. So if you’re running at 60 fps, you will get an update every 16.67ms. Any value to timer.performWithDelay() less than 17 isn’t going to fire multiple times.

The best way to do this will involve an enterFrame listener, and use system.getTimer() (http://docs.coronalabs.com/api/library/system/getTimer.html) which has millisecond resolution.  You get the time from that and update your values. They will still only update once a frame, but you’re getting millisecond values to update your tenths with.

Rob

Thank you Rob.

I am getting warmer.  Here is what I have, but my math is definitely wrong.

[lua]

misc.x = system.getTimer()

misc.b = nil

local function updateTime( event )

    misc.b = (system.getTimer() - misc.x)

    gameText.minutes = math.floor( misc.b / 60 )

    gameText.seconds = math.floor( misc.b / 1000 ) 

    gameText.tenths  = misc.b pick  1000

    gameText.timeDisplay = string.format( “%02d:%02d:%03d”, gameText.minutes, gameText.seconds, gameText.tenths )

    gameText.clockText.text = gameText.timeDisplay

end

local function printTimeSinceStart( event )

    updateTime()

end

Runtime:addEventListener( “enterFrame”, printTimeSinceStart )

[/lua]

 gameText.tenths = misc.b pick 1000

Isn’t valid Lua.

Not sure why the word “pick” is there.

[lua]

 

misc.x = system.getTimer()

misc.b = nil

 

local function updateTime( event )

    misc.b = (system.getTimer() - misc.x)

    gameText.minutes = math.floor( misc.b / 60 )

    gameText.seconds = math.floor( misc.b / 1000 ) 

    gameText.tenths  = misc.b % 1000

    gameText.timeDisplay = string.format( “%02d:%02d:%03d”, gameText.minutes, gameText.seconds, gameText.tenths )

    gameText.clockText.text = gameText.timeDisplay

end

local function printTimeSinceStart( event )

    updateTime()

end

Runtime:addEventListener( “enterFrame”, printTimeSinceStart )

 

[/lua]

[lua]

gameText.minutes = math.floor( misc.b / 60000 )

gameText.seconds = (math.floor( misc.b / 1000 ) ) % 60

gameText.tenths  = misc.b % 1000

[/lua]

I needed the " % 60" on the tail of gameText.seconds.

I have no idea what that % actually means, but in mind it’s the value at which it starts over?

% is the mod command. Essentially what it gives you is the remainder after the number on the left is divided by the number on the right.

So:

4 % 2 = 0

5 % 2 = 1

520 % 250 = 20

The value for milliseconds that you are looking for is:

gameText.tenths = math.fmod(misc.b, 1000)

Thank you all!  I think I’m good to go now.

Anyone ever dump “scores” in to a local leaderboard? 

I’m looking to make a scrollable list of names with times.