前端之家收集整理的这篇文章主要介绍了
cocos2.X版本lua端使用定时器的方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
cocos2.X版本lua端使用定时器的方法:
1、
lua端的使用方式:
local function timerFun(dt)
LuaLog("===================",dt)
end
local scheduler = CCDirector:sharedDirector():getScheduler()
scheduler:scheduleScriptFunc(timerFun,1,false)
2、
C++端的实现:
/** The scheduled script callback will be called every 'interval' seconds.
If paused is YES,then it won't be called until it is resumed.
If 'interval' is 0,it will be called every frame. 如果interval = 0,则每帧都执行
return schedule script entry ID,used for unscheduleScriptFunc().
@js NA
*/
//返回一个entry ID, 在调用unscheduleScriptEntry函数注销定时器时需要用到。
unsigned int scheduleScriptFunc(unsigned int nHandler,float fInterval,bool bPaused);
unsigned int CCScheduler::scheduleScriptFunc(unsigned int nHandler,bool bPaused)
{
//主要是CCSchedulerScriptHandlerEntry内部实现了lua到C++断的封装和转化,这里就不分析了。
CCSchedulerScriptHandlerEntry* pEntry = CCSchedulerScriptHandlerEntry::create(nHandler,fInterval,bPaused);
if (!m_pScriptHandlerEntries)
{
m_pScriptHandlerEntries = CCArray::createWithCapacity(20);
m_pScriptHandlerEntries->retain();
}
m_pScriptHandlerEntries->addObject(pEntry);
return pEntry->getEntryId();
}
/** Unschedule a script entry.
* @js NA
*/
//注销一个定时器回调函数
void unscheduleScriptEntry(unsigned int uScheduleScriptEntryID);
原文链接:https://www.f2er.com/cocos2dx/342918.html