1. 程式人生 > >cocos-lua 定時器使用的2種方式

cocos-lua 定時器使用的2種方式

1.全域性定時器方式Scheduler

例子1

function ActivitySumView:intervalCallBack( dt )
    if  self.time == 0 then
        if  self.entryId then
            cc.Director:getInstance():getScheduler():unscheduleScriptEntry( self.entryId )
            self.entryId = nil
        end
        
        UI.setVisible( self.lbl_time, false )
        return
    end

--    local timeStr = LuaUtils.getTimeStr( self.time )
    local timeStr = LuaUtils:Long2HMS( self.time )
    UI.setString( self.lbl_time, timeStr )
    
    self.time = self.time - 1
end

function ActivitySumView:initTimer()
    if  not self.entryId then
        self:intervalCallBack()
        self.entryId = cc.Director:getInstance():getScheduler():scheduleScriptFunc( function( dt ) self:intervalCallBack( dt ) end, 1, false )
    end
end

例子2

function CampWarView:getRemainTimeStr(strTime)
    return strTime..T("後開放")
end

function CampWarView:intervalCallBack( dt )

    local strTime
    if self.remainTime <= 0 or not self.remainTime then
        UI.setString( self.lbl_gotowar , T("光榮參戰") )
        if  self.entryId then
            cc.Director:getInstance():getScheduler():unscheduleScriptEntry( self.entryId )
            self.entryId = nil
            return
        end
    else
        self.remainTime = self.remainTime - 1
        strTime = LuaUtils:Long2HMS(self.remainTime)
        UI.setString(self.lbl_gotowar, self:getRemainTimeStr(strTime) )
    end  
end

function CampWarView:initTimer()
    if  not self.entryId then
        self:intervalCallBack()
        self.entryId = cc.Director:getInstance():getScheduler():scheduleScriptFunc( function( dt )  
                                                                                            if self.intervalCallBack then
                                                                                                self:intervalCallBack( dt ) 
                                                                                            else
                                                                                                if  self.entryId then
                                                                                                    cc.Director:getInstance():getScheduler():unscheduleScriptEntry( self.entryId )
                                                                                                    self.entryId = nil
                                                                                                    return
                                                                                                 end
                                                                                            end
                                                                                    end, 1, false )
    end
end

2.runAction方式

例子1

self.lbl_gotowar:stopAllActions()
        local action = cc.RepeatForever:create(  
        cc.Sequence:create(  
        cc.DelayTime:create(1),
            cc.CallFunc:create(function() 
                
                if self.remainTime <= 0 or not self.remainTime then--當倒計時為0時,關閉商店
                    strTime = LuaUtils:Long2HMS(0)
                    UI.setString(self.lbl_gotowar, getRemainTimeStr(strTime)) 
                    self.lbl_remainTime:stopAllActions()
                else
                    self.remainTime = self.remainTime - 1
                    strTime = LuaUtils:Long2HMS(self.remainTime)
                    UI.setString(self.lbl_gotowar, getRemainTimeStr(strTime) )
                end
            end)
            ))
         self.lbl_gotowar:runAction(action) 

例子2

self:runAction(cc.Sequence:create(cc.DelayTime:create( 1 ), cc.CallFunc:create( function()      
            UI.alert("掃蕩1次結束")  
        end)))