在使用单个定时器时
local scheduler ,myupdatelocal time=180local function update(dt)
if time<=1 then
time=180
end
time=time-1
local minute=(time-time%60)/60
local seconds=time%60
local showtime=minute..":"..seconds
if type==1 then
self.timecount1:setText(showtime)
print("showtime=="..showtime)
elseif type==2 then
self.timecount2:setText(showtime)
elseif type==3 then
self.timecount2:setText(showtime)
end
-- self:unscheduleUpdate() -- 取消定时器
--scheduler:unscheduleScriptEntry(myupdate) -- 取消定时器
end
scheduler = cc.Director:getInstance():getScheduler()
myupdate = scheduler:scheduleScriptFunc(update, 60.0 / 60.0, false)同时调用多个定时器时 需要在 schedule 设置一个标签 将schedule 加入一个table中
local time1=180
local time2=56
local time3=156
function WorldBoss:mian() for i=1,3 do
if i==1 then
_schedulertabel[i]=self:_schedule(time1,i)
elseif i==2 then
_schedulertabel[i]=self:_schedule(time2,i)
elseif i==3 then
_schedulertabel[i]=self:_schedule(time3,i)
end
endend</pre><pre name="code" class="objc">function WorldBoss:_schedule(time,type)--倒计时(时间,BOSS角色类型)
local scheduler=cc.Director:getInstance():getScheduler()
local schedulerID=nil--定时器标签
if time==0 then
time=180
end
--定时器更新函数
local function anticlockwiseUpdate()
time=time-1
local minute=(time-time%60)/60
local seconds=time%60
local showtime=minute..":"..seconds
--self.timecount1,self.timecount2self.timecount3是三个label
if type==1 then
self.timecount1:setText(showtime)
elseif type==2 then
self.timecount2:setText(showtime)
elseif type==3 then
self.timecount3:setText(showtime)
end
end
schedulerID = scheduler:scheduleScriptFunc(anticlockwiseUpdate,1,false)
return schedulerID
end
本文深入探讨了如何利用单个定时器进行精确计时,并介绍了在不同场景下如何有效管理多个定时器,确保游戏或应用中的计时功能稳定运行。通过实例代码展示定时器的配置、更新逻辑以及如何根据不同类型任务进行时间管理。
1万+

被折叠的 条评论
为什么被折叠?



