cocos2dx学习笔记(定时器)

本文详细解析了Cocos2d-x中的定时器机制,包括如何启用和取消定时器,以及不同类型的定时器如何工作。通过具体的代码示例,介绍了update定时器和schedule定时器的区别及用法。

转载:http://blog.youkuaiyun.com/qq_17749439/article/details/37903435

updata定时器,每一帧都被触发,使用scheduleUpdate方法来启用

schedule定时器,可以设置触发的

在CCNode中

[cpp]  view plain copy
  1. //启用update定时器  
  2. voidCCNode::scheduleUpdate()  
  3. {  
  4.     scheduleUpdateWithPriority(0);  
  5. }  
  6. //启用update定时器,并设定定时器的优先级  
  7. voidCCNode::scheduleUpdateWithPriority(int priority)  
  8. {  
  9.     m_pScheduler->scheduleUpdateForTarget(this, priority, !m_bRunning);  
  10. }  
  11.   
  12. voidCCNode::scheduleUpdateWithPriorityLua(int nHandler,int priority)  
  13. {  
  14.     unscheduleUpdate();  
  15.     m_nUpdateScriptHandler = nHandler;  
  16.     m_pScheduler->scheduleUpdateForTarget(this, priority, !m_bRunning);  
  17. }  
  18. //取消update定时器  
  19. voidCCNode::unscheduleUpdate()  
  20. {  
  21.     m_pScheduler->unscheduleUpdateForTarget(this);  
  22.     if (m_nUpdateScriptHandler)  
  23.     {  
  24.         CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nUpdateScriptHandler);  
  25.         m_nUpdateScriptHandler =0;  
  26.     }  
  27. }  
  28. //添加一个定时器  
  29. //selector参数为定时器的事件函数  
  30. //interval参数为定时器的时间间隔  
  31. //repeat参数为定时时间触发一次后还会再次触发的次数(kCCRepeatForever表示触发无穷多次)  
  32. //delay为第一次触发事件前的延时  
  33. voidCCNode::schedule(SEL_SCHEDULE selector)  
  34. {  
  35.    this->schedule(selector,0.0f,kCCRepeatForever,0.0f);  
  36. }  
  37.   
  38. voidCCNode::schedule(SEL_SCHEDULE selector,float interval)  
  39. {  
  40.    this->schedule(selector, interval,kCCRepeatForever,0.0f);  
  41. }  
  42.   
  43.   
  44. voidCCNode::schedule(SEL_SCHEDULE selector,float interval, unsignedint repeat,float delay)  
  45. {  
  46.     CCAssert( selector,"Argument must be non-nil");  
  47.     CCAssert( interval >=0,"Argument must be positive");  
  48.   
  49.    m_pScheduler->scheduleSelector(selector,this, interval , repeat, delay, !m_bRunning);  
  50. }  
  51. //添加一个定时器,只触发一次  
  52. voidCCNode::scheduleOnce(SEL_SCHEDULE selector,float delay)  
  53. {  
  54.    this->schedule(selector,0.0f,0, delay);  
  55. }  
  56. //取消selector所对应函数的定时器  
  57. voidCCNode::unschedule(SEL_SCHEDULE selector)  
  58. {  
  59.     // explicit nil handling  
  60.    if (selector ==0)  
  61.        return;  
  62.   
  63.    m_pScheduler->unscheduleSelector(selector,this);  
  64. }  
  65. //取消此节点所关联的全部定时器  
  66. voidCCNode::unscheduleAllSelectors()  
  67. {  
  68.     m_pScheduler->unscheduleAllForTarget(this);  
  69. }  
  70. //继续此节点所关联的全部定时器  
  71. voidCCNode::resumeSchedulerAndActions()  
  72. {  
  73.     m_pScheduler->resumeTarget(this);  
  74.     m_pActionManager->resumeTarget(this);  
  75. }  
  76. //暂停此节点所关联的全部定时器  
  77. voidCCNode::pauseSchedulerAndActions()  
  78. {  
  79.     m_pScheduler->pauseTarget(this);  
  80.     m_pActionManager->pauseTarget(this);  
  81. }  







////////////////////////////////////////////////////////////////////////////////////////////////////////////////

而m_pScheduler的类型为CCScheduler,

所以控制游戏定时器的中CCScheduler中。

在游戏循环中调用了m_pScheduler->update(m_fDeltaTime);来控制定时器的调度。


[cpp]  view plain copy
  1. // main loop  
  2. voidCCScheduler::update(float dt)  
  3. {  
  4.     m_bUpdateHashLocked =true;  
  5. //预处理  
  6.     if (m_fTimeScale !=1.0f)  
  7.     {  
  8.         dt *=m_fTimeScale;  
  9.     }  
  10. //枚举所有的update定时器  
  11.     // Iterate over all the Updates' selectors  
  12.    tListEntry *pEntry, *pTmp;  
  13. //优先级小于0的定时器  
  14.     // updates with priority < 0  
  15.    DL_FOREACH_SAFE(m_pUpdatesNegList, pEntry, pTmp)  
  16.     {  
  17.        if ((! pEntry->paused) && (! pEntry->markedForDeletion))  
  18.         {  
  19.             pEntry->target->update(dt);  
  20.         }  
  21.     }  
  22. //优先级等于0的定时器  
  23.     // updates with priority == 0  
  24.    DL_FOREACH_SAFE(m_pUpdates0List, pEntry, pTmp)  
  25.     {  
  26.        if ((! pEntry->paused) && (! pEntry->markedForDeletion))  
  27.         {  
  28.             pEntry->target->update(dt);  
  29.         }  
  30.     }  
  31. //优先级大于0的定时器  
  32.     // updates with priority > 0  
  33.    DL_FOREACH_SAFE(m_pUpdatesPosList, pEntry, pTmp)  
  34.     {  
  35.        if ((! pEntry->paused) && (! pEntry->markedForDeletion))  
  36.         {  
  37.             pEntry->target->update(dt);  
  38.         }  
  39.     }  
  40. //枚举所有的普通定时器  
  41.     // Iterate over all the custom selectors  
  42.     for (tHashTimerEntry *elt =m_pHashForTimers; elt !=NULL; )  
  43.     {  
  44.         m_pCurrentTarget = elt;  
  45.         m_bCurrentTargetSalvaged =false;  
  46.   
  47.         if (!m_pCurrentTarget->paused)  
  48.         {  
  49. //枚举此节点大所有定时器  
  50. //timers数组可能在循环中改变,因此中此处需要小心处理  
  51.             // The 'timers' array may change while inside this loop  
  52.            for (elt->timerIndex =0; elt->timerIndex < elt->timers->num; ++(elt->timerIndex))  
  53.             {  
  54.                 elt->currentTimer = (CCTimer*)(elt->timers->arr[elt->timerIndex]);  
  55.                 elt->currentTimerSalvaged =false;  
  56.   
  57.                 elt->currentTimer->update(dt);  
  58.   
  59.                if (elt->currentTimerSalvaged)  
  60.                 {  
  61.                     // The currentTimer told the remove itself. To prevent the timer from  
  62.                     // accidentally deallocating itself before finishing its step, we retained  
  63.                     // it. Now that step is done, it's safe to release it.  
  64. //该currenttimer告诉自行删除。阻止定时器意外释放本身之前完成的步骤,我们保留它。现在这一步完成之后,释放它的安全。  
  65.                     elt->currentTimer->release();  
  66.                 }  
  67.   
  68.                 elt->currentTimer =NULL;  
  69.             }  
  70.         }  
  71.   
  72.         // elt, at this moment, is still valid  
  73.         // so it is safe to ask this here (issue #490)  
  74.         elt = (tHashTimerEntry *)elt->hh.next;  
  75.   
  76.         // only delete currentTarget if no actions were scheduled during the cycle (issue #481)  
  77.         if (m_bCurrentTargetSalvaged &&m_pCurrentTarget->timers->num ==0)  
  78.         {  
  79.             removeHashElement(m_pCurrentTarget);  
  80.         }  
  81.     }  
  82. //处理脚本引擎相关的事件  
  83.     // Iterate over all the script callbacks  
  84.     if (m_pScriptHandlerEntries)  
  85.     {  
  86.        for (int i =m_pScriptHandlerEntries->count() -1; i >= 0; i--)  
  87.         {  
  88.             CCSchedulerScriptHandlerEntry* pEntry =static_cast<CCSchedulerScriptHandlerEntry*>(m_pScriptHandlerEntries->objectAtIndex(i));  
  89.            if (pEntry->isMarkedForDeletion())  
  90.             {  
  91.                 m_pScriptHandlerEntries->removeObjectAtIndex(i);  
  92.             }  
  93.            elseif (!pEntry->isPaused())  
  94.             {  
  95.                 pEntry->getTimer()->update(dt);  
  96.             }  
  97.         }  
  98.     }  
  99. //清理所有被标记来删除记号的update方法  
  100.     // delete all updates that are marked for deletion  
  101.     // updates with priority < 0  
  102. //优先级小于0的定时器  
  103.    DL_FOREACH_SAFE(m_pUpdatesNegList, pEntry, pTmp)  
  104.     {  
  105.        if (pEntry->markedForDeletion)  
  106.         {  
  107.            this->removeUpdateFromHash(pEntry);  
  108.         }  
  109.     }  
  110.   
  111.     // updates with priority == 0  
  112. //优先级等于0的定时器  
  113.    DL_FOREACH_SAFE(m_pUpdates0List, pEntry, pTmp)  
  114.     {  
  115.        if (pEntry->markedForDeletion)  
  116.         {  
  117.            this->removeUpdateFromHash(pEntry);  
  118.         }  
  119.     }  
  120.   
  121.     // updates with priority > 0  
  122. //优先级大于0的定时器  
  123.    DL_FOREACH_SAFE(m_pUpdatesPosList, pEntry, pTmp)  
  124.     {  
  125.        if (pEntry->markedForDeletion)  
  126.         {  
  127.            this->removeUpdateFromHash(pEntry);  
  128.         }  
  129.     }  
  130.   
  131.     m_bUpdateHashLocked =false;  
  132.   
  133.     m_pCurrentTarget =NULL;  
  134. }  



例子:

[cpp]  view plain copy
  1. this->schedule(schedule_selector(GameScene::updateGame),1.0f);  
  2. void GameScene::updateGame(ccTime dt)  
  3. {  
  4. }   

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值