博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2dx 2.x定时器分析(5)
阅读量:4216 次
发布时间:2019-05-26

本文共 3060 字,大约阅读时间需要 10 分钟。

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

转载地址:http://otsmi.baihongyu.com/

你可能感兴趣的文章
Getting a Result from an Activity
查看>>
Allowing Other Apps to Start Your Activity
查看>>
dev/mem
查看>>
pfn_valid 源码分析
查看>>
dev/kmem 和dev/mem的区别
查看>>
checkbox
查看>>
Sending Simple Data to Other Apps
查看>>
Receiving Simple Data from Other Apps
查看>>
中断API之__tasklet_schedule
查看>>
中断API之enable_irq
查看>>
中断API之disable_irq
查看>>
nova 中的guestfs
查看>>
nova中的localfs
查看>>
utils/rpm_build.sh
查看>>
查看模块参数
查看>>
udev重命名网口
查看>>
pgrep
查看>>
test-definitions/blob/master/toolset/util/parallel_cmds.py
查看>>
中断API之irq_activate
查看>>
中断API之tasklet_disable_nosync/tasklet_trylock/tasklet_unlock
查看>>