cocos2dx定时器详解

来源:互联网 发布:mac os x10.10.5 编辑:程序博客网 时间:2024/05/16 17:43

cocos2dx定时器详解

前言

最近一直在忙cocos2d的Android端游戏,作为从零基础直接上手的菜鸟真的有很多话要说,但在这里,就长话短说了,C++真的是一门有优秀的语言,但是精通好像还蛮难的,我这边的项目需求是用户点击广告,暂时将未解锁的人物角色解锁15分钟供用户使用。这在cocos2d里其实就是用到的是CCSchedule和CCNode类,这个需要结合cocos2d的源码进行分析就会比较简单了。

源码分析

以下是设置定时器

void CCNode::schedule(SEL_SCHEDULE selector){    this->schedule(selector, 0.0f, kCCRepeatForever, 0.0f);}void CCNode::schedule(SEL_SCHEDULE selector, float interval){    this->schedule(selector, interval, kCCRepeatForever, 0.0f);}void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay){    CCAssert( selector, "Argument must be non-nil");    CCAssert( interval >=0, "Argument must be positive");    m_pScheduler->scheduleSelector(selector, this, interval , repeat, delay, !m_bRunning);}//设置一次性的定时器void CCNode::scheduleOnce(SEL_SCHEDULE selector, float delay){    this->schedule(selector, 0.0f, 0, delay);}

取消定时器

void CCNode::unschedule(SEL_SCHEDULE selector){    // explicit nil handling    if (selector == 0)        return;    m_pScheduler->unscheduleSelector(selector, this);}//取消所有的定时器void CCNode::unscheduleAllSelectors(){    m_pScheduler->unscheduleAllForTarget(this);}

更新定时器

void CCNode::scheduleUpdate(){    scheduleUpdateWithPriority(0);}void CCNode::scheduleUpdateWithPriority(int priority){    m_pScheduler->scheduleUpdateForTarget(this, priority, !m_bRunning);}void CCNode::scheduleUpdateWithPriorityLua(int nHandler, int priority){    unscheduleUpdate();    m_nUpdateScriptHandler = nHandler;    m_pScheduler->scheduleUpdateForTarget(this, priority, !m_bRunning);}

取消更新定时器

//void CCNode::unscheduleUpdate(){    m_pScheduler->unscheduleUpdateForTarget(this);    if (m_nUpdateScriptHandler)    {        CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nUpdateScriptHandler);        m_nUpdateScriptHandler = 0;    }}

暂停定时器和动作

void CCNode::pauseSchedulerAndActions(){    m_pScheduler->pauseTarget(this);    m_pActionManager->pauseTarget(this);}

恢复定时器和动作

void CCNode::resumeSchedulerAndActions(){    m_pScheduler->resumeTarget(this);    m_pActionManager->resumeTarget(this);}

代码实例

其实源码分析的比较简单,但是大概知道了那几个方法,其实就知道怎么用了,在实例中,是直接让那个类继承CCNode类,所以是直接调用里面的方法的,代码没整理全,明天补上

总结

原创粉丝点击