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

前端之家收集整理的这篇文章主要介绍了cocos2dx 2.x定时器分析(1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、创建全局定时器,bool CCDirector::init(void)函数中,调用

  1. // scheduler
  2. m_pScheduler = new CCScheduler();

  1. 2、在每帧渲染前,都会调用CCSchedulerupdate方法
  2. // Draw the Scene
  3. void CCDirector::drawScene(void)
  4. {
  5. // calculate "global" dt
  6. calculateDeltaTime();
  7.  
  8. //tick before glClear: issue #533
  9. if (! m_bPaused) //调用定时器的update方法
  10. {
  11. m_pScheduler->update(m_fDeltaTime);
  12. }
  13.  
  14. }

  1. 3
  2. CCScheduler中有几个很重要的成员变量,
  3. //每帧都要调用的定时器
  4. struct _listEntry *m_pUpdatesNegList; // list of priority < 0 //存放priority < 0的Update,优先级最高
  5. struct _listEntry *m_pUpdates0List; // list priority == 0 //存放priority == 0的Update
  6. struct _listEntry *m_pUpdatesPosList; // list priority > 0 //存放priority > 0的Update,优先级最低
  7.  
  8. //方便快速查找
  9. struct _hashUpdateEntry *m_pHashForUpdates; // hash used to fetch quickly the list entries for pause,delete,etc
  10.  
  11. // Used for "selectors with interval",设置了时间间隔的定时器列表
  12. struct _hashSelectorEntry *m_pHashForTimers; //

4、
  1. CCSchedulerupdate函数很大,主要是几个循环。

  1. // main loop
  2. void CCScheduler::update(float dt)
  3. {
  4. m_bUpdateHashLocked = true;
  5.  
  6. if (m_fTimeScale != 1.0f)
  7. {
  8. dt *= m_fTimeScale;
  9. }
  10.  
  11. // Iterate over all the Updates' selectors
  12. tListEntry *pEntry,*pTmp;
  13.  
  14. // updates with priority < 0 优先调用
  15. // pEntry->target->update(dt) 调用target的update方法,例继承与CCNode的类
  16. //可以重写 CCNode的 update方法
  17. /*
  18. * Update method will be called automatically every frame if "scheduleUpdate" is called,and the node is "live"
  19. */
  20. //virtual void update(float delta);
  21.  
  22.  
  23. DL_FOREACH_SAFE(m_pUpdatesNegList,pEntry,pTmp)
  24. {
  25. if ((! pEntry->paused) && (! pEntry->markedForDeletion))
  26. {
  27. pEntry->target->update(dt);
  28. }
  29. }
  30.  
  31. // updates with priority == 0
  32. DL_FOREACH_SAFE(m_pUpdates0List,pTmp)
  33. {
  34. if ((! pEntry->paused) && (! pEntry->markedForDeletion))
  35. {
  36. pEntry->target->update(dt);
  37. }
  38. }
  39.  
  40. // updates with priority > 0
  41. DL_FOREACH_SAFE(m_pUpdatesPosList,pTmp)
  42. {
  43. if ((! pEntry->paused) && (! pEntry->markedForDeletion))
  44. {
  45. pEntry->target->update(dt);
  46. }
  47. }
  48.  
  49. // Iterate over all the custom selectors
  50. //遍历有时间间隔的定时器列表,调用相应的方法
  51. for (tHashTimerEntry *elt = m_pHashForTimers; elt != NULL; )
  52. {
  53. m_pCurrentTarget = elt;
  54. m_bCurrentTargetSalvaged = false;
  55.  
  56. if (! m_pCurrentTarget->paused)
  57. {
  58. // The 'timers' array may change while inside this loop
  59. for (elt->timerIndex = 0; elt->timerIndex < elt->timers->num; ++(elt->timerIndex))
  60. {
  61. elt->currentTimer = (CCTimer*)(elt->timers->arr[elt->timerIndex]);
  62. elt->currentTimerSalvaged = false;
  63.  
  64. elt->currentTimer->update(dt);
  65.  
  66. if (elt->currentTimerSalvaged)
  67. {
  68. // The currentTimer told the remove itself. To prevent the timer from
  69. // accidentally deallocating itself before finishing its step,we retained
  70. // it. Now that step is done,it's safe to release it.
  71. elt->currentTimer->release();
  72. }
  73.  
  74. elt->currentTimer = NULL;
  75. }
  76. }
  77.  
  78. // elt,at this moment,is still valid
  79. // so it is safe to ask this here (issue #490)
  80. elt = (tHashTimerEntry *)elt->hh.next;
  81.  
  82. // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
  83. if (m_bCurrentTargetSalvaged && m_pCurrentTarget->timers->num == 0)
  84. {
  85. removeHashElement(m_pCurrentTarget);
  86. }
  87. }
  88.  
  89. // Iterate over all the script callbacks
  90. //遍历针对脚本的定时器列表
  91. if (m_pScriptHandlerEntries)
  92. {
  93. for (int i = m_pScriptHandlerEntries->count() - 1; i >= 0; i--)
  94. {
  95. CCSchedulerScriptHandlerEntry* pEntry = static_cast<CCSchedulerScriptHandlerEntry*>(m_pScriptHandlerEntries->objectAtIndex(i));
  96. if (pEntry->isMarkedForDeletion())
  97. {
  98. m_pScriptHandlerEntries->removeObjectAtIndex(i);
  99. }
  100. else if (!pEntry->isPaused())
  101. {
  102. pEntry->getTimer()->update(dt);
  103. }
  104. }
  105. }
  106.  
  107. // delete all updates that are marked for deletion
  108. // updates with priority < 0
  109. //删除一些没用定时器
  110. DL_FOREACH_SAFE(m_pUpdatesNegList,pTmp)
  111. {
  112. if (pEntry->markedForDeletion)
  113. {
  114. this->removeUpdateFromHash(pEntry);
  115. }
  116. }
  117.  
  118. // updates with priority == 0
  119. DL_FOREACH_SAFE(m_pUpdates0List,pTmp)
  120. {
  121. if (pEntry->markedForDeletion)
  122. {
  123. this->removeUpdateFromHash(pEntry);
  124. }
  125. }
  126.  
  127. // updates with priority > 0
  128. DL_FOREACH_SAFE(m_pUpdatesPosList,pTmp)
  129. {
  130. if (pEntry->markedForDeletion)
  131. {
  132. this->removeUpdateFromHash(pEntry);
  133. }
  134. }
  135.  
  136. m_bUpdateHashLocked = false;
  137.  
  138. m_pCurrentTarget = NULL;
  139. }

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