这个类,在这里进行下说明。
1、第一次用到在CCScheduler::scheduleSelector函数中,一个target需要
添加多个时间间隔定时器时会出现,每一个CCTimer代表一个时间间隔定时器,
包含一个定时器的全部信息。
初始化方法: /** Initializes a timer with a target,a selector and an interval in seconds,repeat in number of times to repeat,delay in seconds. * @lua NA */ bool initWithTarget(CCObject *pTarget,SEL_SCHEDulE pfnSelector,float fSeconds,unsigned int nRepeat,float fDelay);
2、第二次是在脚本定时器中使用,unsigned int CCScheduler::scheduleScriptFunc(unsigned int nHandler,float fInterval,bool bPaused)脚本定时器会用到CCSchedulerScriptHandlerEntry,这个类中有一个成员变量是CCTimer,会使用方法: /** Initializes a timer with a script callback function and an interval in seconds. */ bool initWithScriptHandler(int nHandler,float fSeconds); 3、voID CCScheduler::update(float dt)函数中,会调用相应CCTimer的update方法, -->> voID CCTimer::update(float dt){ if (m_fElapsed == -1) { m_fElapsed = 0; m_uTimesExecuted = 0; } else { if (m_bRunForever && !m_bUseDelay) {//standard timer usage m_fElapsed += dt; if (m_fElapsed >= m_fInterval) { //执行回调函数 if (m_pTarget && m_pfnSelector) { (m_pTarget->*m_pfnSelector)(m_fElapsed); } //执行脚本回调函数 if (m_nScriptHandler) { ccScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(m_nScriptHandler,m_fElapsed); } m_fElapsed = 0; } } else {//advanced usage m_fElapsed += dt; if (m_bUseDelay) { if( m_fElapsed >= m_fDelay ) { if (m_pTarget && m_pfnSelector) { (m_pTarget->*m_pfnSelector)(m_fElapsed); } if (m_nScriptHandler) { ccScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(m_nScriptHandler,m_fElapsed); } m_fElapsed = m_fElapsed - m_fDelay; m_uTimesExecuted += 1; m_bUseDelay = false; } } else { if (m_fElapsed >= m_fInterval) { if (m_pTarget && m_pfnSelector) { (m_pTarget->*m_pfnSelector)(m_fElapsed); } if (m_nScriptHandler) { ccScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(m_nScriptHandler,m_fElapsed); } m_fElapsed = 0; m_uTimesExecuted += 1; } } if (!m_bRunForever && m_uTimesExecuted > m_uRepeat) { //unschedule timer CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(m_pfnSelector,m_pTarget); } } }}总结
以上是内存溢出为你收集整理的cocos2dx 2.x定时器分析(5)全部内容,希望文章能够帮你解决cocos2dx 2.x定时器分析(5)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)