您的位置:首页 > 其它

在一个界面使用定时器后,退出界面后,一定要记得取消定时器

2015-07-30 10:45 337 查看
一个游戏界面要做一个倒计时,做完后,退出界面,再进去时,游戏崩溃。

倒计时的定时器使用

local function onInterval(dt)
local time = SGGJ_Club_Total.bossleft
if tonumber(time) >= 0 then
--里面有其他与此知识点操作都省略....
local hour = math.floor((time) / 3600)
local min = math.floor((time % 3600) / 60)
local second = (time % 3600) % 60
daojishiText:setString(string.format("%02d : %02d : %02d",hour , min, second))
else
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.scheduleId)
self.scheduleId = nil
end
end
--这里第一次调用,让一进界面,倒计时数据就设置后了,否则会有1秒钟的混乱数据
onInterval()
--每秒调用一次onInterval函数
self.scheduleId = cc.Director:getInstance():getScheduler():scheduleScriptFunc(onInterval, 1, false)


此时已经做好了倒计时定时器,但是当你退出这个界面是,假如时间还没到(unscheduleScriptEntry这个函数就不会得到调用)

所以我在init函数中

local function nodeEvent(event)
if "enter" == event then
--enter
elseif "exit" == event then
if self.scheduleId then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.scheduleId)
self.scheduleId = nil
end
end
end
self:registerScriptHandler(nodeEvent)


当这个界面退出时,会取消定时器
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: