cocos2dx 2.x定时器分析(5)

前端之家收集整理的这篇文章主要介绍了cocos2dx 2.x定时器分析(5)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
CCTimer类,上面几篇定时器的分析的文章中,多次用到了
这个类,在这里进行下说明。

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);
            }
        }
    }
}

猜你在找的Cocos2d-x相关文章