2016-04-20 82 views
1

下面的倒計時時鐘從秒,分鐘,日,月,年,我怎麼會增加毫秒到這個?倒計時如何添加毫秒

local secondsLeft = 3.154e + 7 
-- one year 


local clockText = display.newText("00.00:00:00:00", display.contentCenterX, 80, "font1.ttf", 60) 



sceneGroup:insert(bg) 
sceneGroup:insert(title) 
sceneGroup:insert(summary) 
sceneGroup:insert(clockText) 



sceneGroup:insert(clockText) 
local SECONDS_IN_HOUR = 60 * 60 
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR 
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY 
-- assuming an average of 30 days per month 
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY 

local function updateTime() 
    -- decrement the number of seconds 

    secondsLeft = secondsLeft - 1 


    -- time is tracked in seconds. We need to convert it to minutes and seconds 


    local years = math.floor((secondsLeft/SECONDS_IN_YEAR) % 365) 
    local months = math.floor((secondsLeft/SECONDS_IN_MONTH) % 12) 
    local days = math.floor((secondsLeft/SECONDS_IN_DAY) % 30) 
    local hours = math.floor((secondsLeft/SECONDS_IN_HOUR) % 24) 
    local minutes = math.floor((secondsLeft/60) % 60) 
    local seconds = secondsLeft % 60 




    -- make it a string using string format. 
    local timeDisplay = string.format("%02d·%02d·%02d·%02d·%02d·%02d", years, months, days, hours, minutes, seconds) 
    clockText.text = timeDisplay 
end 
local countDownTimer = timer.performWithDelay(100, updateTime, secondsLeft) 
+0

嘗試'運行:的addEventListener( 「enterFrame事件」,函數(事件),印刷(event.time)結束)'[文檔這裏( https://docs.coronalabs.com/api/event/enterFrame/time.html) – Albert

回答

0

下面是一個例子,也許對你有用:

local sceneGroup = display.newGroup() 

local totalSecond = 5 * 1000 -- 5 sceonds 
local clockText = display.newText("", display.contentCenterX, 80, native.systemFontBold, 30) 
--sceneGroup:insert(bg) 
--sceneGroup:insert(title) 
--sceneGroup:insert(summary) 
sceneGroup:insert(clockText) 



sceneGroup:insert(clockText) 
local SECONDS_IN_HOUR = 60 * 60 
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR 
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY -- assuming an average of 30 days per month 
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY 



function updateTime(msLeft) 
    -- decrement the number of seconds 
    if totalSecond >= msLeft then 
     local secondsLeft = totalSecond - msLeft 
     -- time is tracked in seconds. We need to convert it to minutes and seconds 
     local years = math.floor((secondsLeft/1000/SECONDS_IN_YEAR) % 365) 
     local months = math.floor((secondsLeft/1000/SECONDS_IN_MONTH) % 12) 
     local days = math.floor((secondsLeft/1000/SECONDS_IN_DAY) % 30) 
     local hours = math.floor((secondsLeft/1000/SECONDS_IN_HOUR) % 24) 
     local minutes = math.floor((secondsLeft/1000/60) % 60) 
     local seconds = math.floor(secondsLeft/1000 % 60) 
     local msceonds = secondsLeft % 1000 
     -- make it a string using string format. 
     local timeDisplay = string.format("%02dY·%02dM·%02dD·%02dH·%02dM·%02dS·%03dMS", years, months, days, hours, minutes, seconds, msceonds) 
     clockText.text = timeDisplay 
    else 
     clockText.text = "Time's up!" 
     Runtime:removeEventListener("enterFrame", enterFrameListener) 
    end 
end 

function enterFrameListener(e) 
    updateTime(e.time) 
end 

Runtime:addEventListener("enterFrame", enterFrameListener)