Lua 實現倒計時功能

2021-06-18 21:50:25 字數 2192 閱讀 7929

--lua 實現倒計時功能

local size = ccdirector:shareddirector():getwinsize()

local scheduler = ccdirector:shareddirector():getscheduler()

local run_logic = nil

--時 分 秒 數值

local hour = 1

local minute = 0

local second = 0

--將int型別轉換為string型別

hour = hour..""

minute = minute..""

second = second..""

--當顯示數字為個位數時,前位用0補上

if string.len(hour) == 1 then

hour = "0"..hour

endif string.len(minute) == 1 then

minute = "0"..minute

endif string.len(second) == 1 then

second = "0"..second

end--建立時間標籤用以顯示

local time = cclabelttf:create("倒計時:"..hour..":"..minute..":"..second,"thonburi",35)

time:setcolor(ccc3(0,255,0))

time:setposition(ccp(size.width*0.5,size.height*0.6))

mainlayer:addchild(time,1,123)

--倒計時更新函式

local function anticlockwiseupdate()

local time = mainlayer:getchildbytag(123)

time = tolua.cast(time,"cclabelttf")

second = second-1

if second == -1 then

if minute ~= -1 or hour ~= -1 then

minute = minute-1

second = 59

if minute == -1 then

if hour ~= -1 then

hour = hour-1

minute = 59

if hour == -1 then

--倒計時結束停止更新

if run_logic ~= nil then

scheduler:unschedulescriptentry(run_logic)

run_logic = nil

endsecond = 0

minute = 0

hour = 0

time:setcolor(ccc3(255,0,0)) --以紅色標識結束

endend

endend

endsecond = second..""

minute = minute..""

hour = hour..""

if string.len(second) == 1 then

second = "0"..second

endif string.len(minute) == 1 then

minute = "0"..minute

endif string.len(hour) == 1 then

hour = "0"..hour

endtime:setstring("倒計時:"..hour..":"..minute..":"..second)

end--開始倒計時 每1秒呼叫一次anticlockwiseupdate方法

run_logic = scheduler:schedulescriptfunc(anticlockwiseupdate,1,false)

local scene = ccscene:create()

scene:addchild(mainlayer)

ccdirector:shareddirector():runwithscene(scene)

實現倒計時功能

一 php time1 strtotime date y m d h i s time2 strtotime 2017 01 01 00 00 00 time3 strtotime 2017 05 01 sub1 ceil time2 time1 3600 60 60 sub2 ceil time3...

lua 計算倒計時,天 周 月倒計時

通過伺服器時間 獲得明天零點的時間戳 function getnextdayzerotime timenum 獲得當前伺服器的時間 local t1 timenum if not t1 then t1 systemtime end 獲得時間格式 local tab os.date t t1 tab....

Unity實現倒計時功能

有兩種思路可以實現倒計時,乙個是update,另乙個是協程。這裡只展示核心的演算法思路,有收穫的還請點個贊哦 update 首先定義三個變數,訪問許可權按需求設定 float gametime 遊戲總時間,int或者float都可,單位為秒 float timeleft 遊戲剩餘時間,單位為秒 fl...