Hi, how do I make a timer exactly like this in corona? with milliseconds
https://www.youtube.com/watch?v=9cQT4urTlXM
Try this:
local cx = display.contentCenterX local cy = display.contentCenterY local getTimer = system.getTimer local mFloor = math.floor local mAbs = math.abs local function round(val, n) if (n) then return math.floor( (val \* 10^n) + 0.5) / (10^n) else return math.floor(val+0.5) end end local timerText = display.newText( "", cx, cy, native.systemFont, 22 ) timerText.startTime = getTimer() timerText.enterFrame = function( self ) local dt = getTimer() - self.startTime local remainder = mAbs(round(dt/1000) - round(dt/1000,4)) remainder = round( remainder, 4 ) print(remainder) local seconds = round(dt/1000) local nHours = string.format("%02.f", mFloor(seconds/3600)) local nMins = string.format("%02.f", mFloor(seconds/60 - (nHours\*60))) local nSecs = string.format("%02.4f", mFloor(seconds - nHours\*3600 - nMins \*60) + remainder) self.text = nHours..":"..nMins..":"..nSecs end Runtime:addEventListener( "enterFrame", timerText )
Keep in mind, Corona SDK updates the screen every 1/30th of a second (33 milliseconds) or every 1/60th of a second (16.7 milliseconds) on a “best effort” attempt. That is somethings could cause our frame timer to delay. Given this you cannot really display anything more accurate than that. You can compute it, you just can’t display it.
Rob
Try this:
local cx = display.contentCenterX local cy = display.contentCenterY local getTimer = system.getTimer local mFloor = math.floor local mAbs = math.abs local function round(val, n) if (n) then return math.floor( (val \* 10^n) + 0.5) / (10^n) else return math.floor(val+0.5) end end local timerText = display.newText( "", cx, cy, native.systemFont, 22 ) timerText.startTime = getTimer() timerText.enterFrame = function( self ) local dt = getTimer() - self.startTime local remainder = mAbs(round(dt/1000) - round(dt/1000,4)) remainder = round( remainder, 4 ) print(remainder) local seconds = round(dt/1000) local nHours = string.format("%02.f", mFloor(seconds/3600)) local nMins = string.format("%02.f", mFloor(seconds/60 - (nHours\*60))) local nSecs = string.format("%02.4f", mFloor(seconds - nHours\*3600 - nMins \*60) + remainder) self.text = nHours..":"..nMins..":"..nSecs end Runtime:addEventListener( "enterFrame", timerText )
Keep in mind, Corona SDK updates the screen every 1/30th of a second (33 milliseconds) or every 1/60th of a second (16.7 milliseconds) on a “best effort” attempt. That is somethings could cause our frame timer to delay. Given this you cannot really display anything more accurate than that. You can compute it, you just can’t display it.
Rob