cocos lua timer related

Local timers used, or more, logical judgment and are based on many of the game's performed for each frame. Quick scheduler to schedule the package file in Lua. If you are new to Quick, it may according to the official API to write a timer is an error, suggesting schedule is a nil value, because the other modules in the initialization are loaded, with the exception of the scheduler did not contain into, so when in use, the first thing is the introduction of this module,

local scheduler = require("framework.scheduler")

You can look at the rest of the API to write, write quick before the timer or re-review the cocos2dx native lua written for timer

 

Each frame call

void scheduleUpdateWithPriority (int priority)

void scheduleUpdateWithPriorityLua (int nHandler,int priority)

Specify the interval call

unsigned int scheduleScriptFunc (unsigned int nHandler, float fInterval, bool bPaused)

There timer event canceled

void unscheduleScriptEntry (unsigned int uScheduleScriptEntryID)

Quick The scheduler primarily the encapsulation of the latter two functions. In cocos use c ++, we use a timer, each frame is nothing more than call, call time interval countless times, every time a specified number of calls, call time interval, cancel call a few.

 

Each frame call

local time = 0  

local function update(dt)  

    time = time + 1  

    label:setString(string.format("%d", time))  

end  

scheduler.scheduleUpdateGlobal(update)

 

 

Call a certain time interval

--

local time = 0  

local function onInterval(dt)  

    time = time + 1  

    label:setString(string.format("%d", time))  

end  

scheduler.scheduleGlobal(onInterval, 1)

 

 

Interval called once, this package good, very popular

local time = 0  

local function onInterval(dt)  

    time = time + 1  

    label:setString(string.format("%d", time))  

    print("over")  

end  

   

scheduler.performWithDelayGlobal(onInterval, 1)

 

 

 

  Look at how this can be achieved

  

function scheduler.performWithDelayGlobal(listener, time)  

    local handle  

    handle = sharedScheduler:scheduleScriptFunc(function()  

        scheduler.unscheduleGlobal(handle)  

        listener()  

    end, time, false)  

    return handle  

end

 

 

In fact, after a certain time interval, stop it, and then perform a callback on it. The last package is to stop the timer

scheduler.unscheduleGlobal()

 

 

 

Its argument is that the front of the timer returns a handle, so if you need to stop off at the back, remember to leave a return value is created just fine.

---------------------

But in the game, we might do a countdown, that is, the number of calls the specified time intervals, this is not encapsulated in the quick, but we still can achieve about yourself, the principle is very simple, once each execution the number of dollars, to reach the specified number of times to stop the timer,

local handle  

local interval = 1  

local repeatIndex = 3  

local index = 0  

local sharedScheduler = CCDirector:sharedDirector():getScheduler()  

handle = sharedScheduler:scheduleScriptFunc(function()  

    index = index + 1  

    label:setString(string.format("%d", index))  

    if index >= repeatIndex then  

        scheduler.unscheduleGlobal(handle)  

        print("over")  

    end  

end, interval, false)

 

Guess you like

Origin www.cnblogs.com/taotaodmw/p/12457406.html