在使用单个定时器时
local scheduler ,myupdate
local time=180
local 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
end
end
</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