How best to do a timer

Hello,

i’m currently trying to build a time that starts at 00:00 and counts up based no duration of play.

I have managed to do it and it counts up but seems to go nuts on me when i test my game.

For example i will start the game and it will count, but when an actor is spawned it will skip a few seconds up and then go back down to where it should be. My code is below.

Is there a different way of doing it?

[lua]local runningTime = display.newText( “00:00”, 115, 105, “ArialRoundedMTBold”, 26 )
runningTime:setTextColor( 255, 255, 255 )
runningTime.x = 60
runningTime.y = 20

local t1 = system.getTimer()

function runningTime:timer ( event )
local t2 = system.getTimer()
local ms = t2 - t1
t1 = ms - t2

local floor = math.floor
local seconds = floor(ms / 1000)
local minutes = floor(seconds / 60); seconds = floor(seconds % 60)
local hours = floor(minutes / 60); minutes = floor(minutes % 60)

formattedTime = string.format("%02d:%02d", minutes, seconds)
self.text = formattedTime

return formattedTime

end

timer1 = timer.performWithDelay(1000, runningTime, 0)[/lua]

Thanks [import]uid: 13059 topic_id: 12401 reply_id: 312401[/import]