【Cocos2d-x 3.x】 调度器Scheduler类源码分析

前端之家收集整理的这篇文章主要介绍了【Cocos2d-x 3.x】 调度器Scheduler类源码分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

非个人的全部理解,部分摘自cocos官网教程,感谢cocos官网。



在<CCScheduler.h>头文件中,定义了关于调度器的五个类:Timer,TimerTargetSelector,TimerTargetCallback,TimerScriptHandler和Scheduler,Timer和Scheduler直接继承于Ref类,TimerTargetSelector,TimerTargetCallback和TimerScriptHandler继承自Timer类。


先看看Timer类:

  1. class CC_DLL Timer : public Ref
  2. {
  3. protected:
  4. Timer();
  5. public:
  6. /** get interval in seconds */
  7. inline float getInterval() const { return _interval; };
  8. /** set interval in seconds */
  9. inline void setInterval(float interval) { _interval = interval; };
  10. void setupTimerWithInterval(float seconds,unsigned int repeat,float delay);
  11. virtual void trigger() = 0;
  12. virtual void cancel() = 0;
  13. /** triggers the timer */
  14. void update(float dt);
  15. protected:
  16. Scheduler* _scheduler; // weak ref
  17. float _elapsed; // 渡过的时间
  18. bool _runForever; // 标记是否永远运行
  19. bool _useDelay; // 标记是否使用延迟
  20. unsigned int _timesExecuted; // 记录已经执行了多少次
  21. unsigned int _repeat; //0 = once,1 is 2 x executed 定义要执行的次数,0表示执行1次, 1表示执行2次
  22. float _delay; // 延迟的时间
  23. float _interval; // 时间间隔
  24. };

类中定义了一个Scheduler指针变量 _scheduler,根据注释可以看出,这是一个弱引用,弱引用不会增加它所引用的对象的引用计数;

根据分析Timer类的成员变量,可以知道这是一个用来描述计时器的类;

  1. 每隔 _interval 来触发一次;

  2. _useDelay可以设置定时器触是否使用延迟; _delay是延迟时间;

  3. _repeat可以设置定时器触发的次数; _runforever设置定时器永远执行。

然后看一下Timer类的update函数

  1. void Timer::update(float dt) //参数dt表示距离上一次update调用的时间间隔
  2. {
  3. if (_elapsed == -1)// 如果 _elapsed值为-1表示这个定时器是第一次进入到update方法 并做初始化操作。
  4. {
  5. _elapsed = 0;
  6. _timesExecuted = 0;
  7. }
  8. else
  9. {
  10. if (_runForever && !_useDelay)
  11. {//standard timer usage
  12. _elapsed += dt; //累计渡过的时间。
  13. if (_elapsed >= _interval)
  14. {
  15. trigger();
  16. _elapsed = 0; //触发后将_elapsed清除为0,这里可能会有一小点的问题,因为 _elapsed值有可能大于_interval这里没有做冗余处理,所以会吞掉一些时间,比如 1秒执行一次,而10秒内可能执行的次数小于10,吞掉多少与update调用的频率有关系。
  17. }
  18. }
  19. else
  20. {//advanced usage
  21. _elapsed += dt;
  22. if (_useDelay)
  23. {
  24. if( _elapsed >= _delay )
  25. {
  26. trigger();
  27. _elapsed = _elapsed - _delay;//延迟执行的计算
  28. _timesExecuted += 1;
  29. _useDelay = false; //延迟已经过了,清除_useDelay标记
  30. }
  31. }
  32. else
  33. {
  34. if (_elapsed >= _interval)
  35. {
  36. trigger();
  37. _elapsed = 0;
  38. _timesExecuted += 1;
  39. }
  40. }
  41. if (!_runForever && _timesExecuted > _repeat)//触发的次数已经满足了_repeat的设置就取消定时器。
  42. { //unschedule timer
  43. cancel();
  44. }
  45. }
  46. }
  47. }

在update函数里,调用了trigger和cancel函数,trigger是触发函数,cancel是取消定时器。


然后继续看<Scheduler.h>,是TimerTargetSelector,它继承自Timer:

  1. class CC_DLL TimerTargetSelector : public Timer
  2. {
  3. public:
  4. TimerTargetSelector();
  5.  
  6. /** Initializes a timer with a target,a selector and an interval in seconds,repeat in number of times to repeat,delay in seconds. */
  7. bool initWithSelector(Scheduler* scheduler,SEL_SCHEDULE selector,Ref* target,float seconds,float delay);
  8. inline SEL_SCHEDULE getSelector() const { return _selector; };
  9. virtual void trigger() override;
  10. virtual void cancel() override;
  11. protected:
  12. Ref* _target; // 关联一个Ref对象,应该为执行定时器的对象
  13. SEL_SCHEDULE _selector; // _selector是一个函数,是定时器触发时的回调函数
  14. };

然后看看TimerTargetSelector的trigger和cancel函数,它重载了父类Timer的同名虚函数
  1. void TimerTargetSelector::trigger()
  2. {
  3. if (_target && _selector)
  4. {
  5. (_target->*_selector)(_elapsed);
  6. }
  7. }
  8.  
  9. void TimerTargetSelector::cancel()
  10. {
  11. _scheduler->unschedule(_selector,_target);
  12. }

可以看出,在trigger中,执行了_selector这个回调函数,cancel函数调用了unscheduler函数来结束,稍后分析。


继续看下一个类TimerTargetCallback:

  1. class CC_DLL TimerTargetCallback : public Timer
  2. {
  3. public:
  4. TimerTargetCallback();
  5. /** Initializes a timer with a target,a lambda and an interval in seconds,delay in seconds. */
  6. bool initWithCallback(Scheduler* scheduler,const ccSchedulerFunc& callback,void *target,const std::string& key,float delay);
  7. /**
  8. * @js NA
  9. * @lua NA
  10. */
  11. inline const ccSchedulerFunc& getCallback() const { return _callback; };
  12. inline const std::string& getKey() const { return _key; };
  13. virtual void trigger() override;
  14. virtual void cancel() override;
  15. protected:
  16. void* _target;
  17. ccSchedulerFunc _callback;
  18. std::string _key;
  19. };

TimerTargetCallback 和TimerTargetSelector类似,然后可以看看它的trigger和cancel:
  1. void TimerTargetCallback::trigger()
  2. {
  3. if (_callback)
  4. {
  5. _callback(_elapsed);
  6. }
  7. }
  8.  
  9. void TimerTargetCallback::cancel()
  10. {
  11. _scheduler->unschedule(_key,_target);
  12. }

trigger就是调用了回调函数并将_elapsed传进去,cancel和TimerTargetSelector和cancel一样。

然后还有个跟脚本相关的,暂时不会~


然后现在可以看看Scheduler类了,在Scheduler之前声明了几种结构体:

  1. struct _listEntry;
  2. struct _hashSelectorEntry;
  3. struct _hashUpdateEntry;
  4.  
  5. #if CC_ENABLE_SCRIPT_BINDING
  6. class SchedulerScriptHandlerEntry;

估计和Scheduler的数据结构有关,接着看看Scheduler的数据:
  1. float _timeScale; // 速度控制,1.0f为正常速度,大于1表示快放,小于1表示慢放
  2. //
  3. // "updates with priority" stuff
  4. //
  5. struct _listEntry *_updatesNegList; // list of priority < 0
  6. struct _listEntry *_updates0List; // list priority == 0
  7. struct _listEntry *_updatesPosList; // list priority > 0
  8. struct _hashUpdateEntry *_hashForUpdates; // hash used to fetch quickly the list entries for pause,delete,etc
  9.  
  10. // Used for "selectors with interval"
  11. struct _hashSelectorEntry *_hashForTimers;
  12. struct _hashSelectorEntry *_currentTarget;
  13. bool _currentTargetSalvaged;
  14. // If true unschedule will not remove anything from a hash. Elements will only be marked for deletion.
  15. bool _updateHashLocked;
  16. #if CC_ENABLE_SCRIPT_BINDING
  17. Vector<SchedulerScriptHandlerEntry*> _scriptHandlerEntries;
  18. #endif
  19. // Used for "perform Function"
  20. std::vector<std::function<void()>> _functionsToPerform;
  21. std::mutex _performMutex;

Scheduler定义了一些和调度器相关的一些“容器”,后面慢慢分析。

来看看Scheduler的构造和析构函数

  1. Scheduler::Scheduler(void)
  2. : _timeScale(1.0f),_updatesNegList(nullptr),_updates0List(nullptr),_updatesPosList(nullptr),_hashForUpdates(nullptr),_hashForTimers(nullptr),_currentTarget(nullptr),_currentTargetSalvaged(false),_updateHashLocked(false)
  3. #if CC_ENABLE_SCRIPT_BINDING,_scriptHandlerEntries(20)
  4. #endif
  5. {
  6. // I don't expect to have more than 30 functions to all per frame
  7. _functionsToPerform.reserve(30);
  8. }
  9.  
  10. Scheduler::~Scheduler(void)
  11. {
  12. unscheduleAll();
  13. }
在构造函数中,官方给出的建议是每帧中的回调函数个数不要超过30个。析构函数调用了uncheduleAll。


然后看看Scheduler中非常重要的函数schedule,它有几个重载版本:

  1. void Scheduler::schedule(SEL_SCHEDULE selector,Ref *target,float interval,float delay,bool paused)
  2. {
  3. CCASSERT(target,"Argument target must be non-nullptr");
  4. tHashTimerEntry *element = nullptr;
  5. HASH_FIND_PTR(_hashForTimers,&target,element);
  6. if (! element)
  7. {
  8. element = (tHashTimerEntry *)calloc(sizeof(*element),1);
  9. element->target = target;
  10. HASH_ADD_PTR(_hashForTimers,target,element);
  11. // Is this the 1st element ? Then set the pause level to all the selectors of this target
  12. element->paused = paused;
  13. }
  14. else
  15. {
  16. CCASSERT(element->paused == paused,"");
  17. }
  18. if (element->timers == nullptr)
  19. {
  20. element->timers = ccArrayNew(10);
  21. }
  22. else
  23. {
  24. for (int i = 0; i < element->timers->num; ++i)
  25. {
  26. TimerTargetSelector *timer = dynamic_cast<TimerTargetSelector*>(element->timers->arr[i]);
  27. if (timer && selector == timer->getSelector())
  28. {
  29. CCLOG("CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to %.4f",timer->getInterval(),interval);
  30. timer->setInterval(interval);
  31. return;
  32. }
  33. }
  34. ccArrayEnsureExtraCapacity(element->timers,1);
  35. }
  36. TimerTargetSelector *timer = new (std::nothrow) TimerTargetSelector();
  37. timer->initWithSelector(this,selector,interval,repeat,delay);
  38. ccArrayAppendObject(element->timers,timer);
  39. timer->release();
  40. }


调用了 HASH_FIND_PTR(_hashForTimers,element); 这行代码的含义是在 _hashForTimers 这个数组中找与&target相等的元素,用element来返回。
而_hashForTimers是一个链表。@H_404_104@接下来的if判断是判断element的值,看看是不是已经在_hashForTimers链表里面,如果不在那么分配内存创建了一个新的结点并且设置了pause状态;

再下面的if判断的含义是,检查当前这个_target的定时器列表状态,如果为空那么给element->timers分配了定时器空间@H_404_104@如果这个_target的定时器列表不为空,那么检查列表里是否已经存在了 selector 的回调,如果存在那么更新它的间隔时间,并退出函数

  1. ccArrayEnsureExtraCapacity(element->timers,1);

这行代码是给 ccArray分配内存,确定能再容纳一个timer。
函数的最后四行代码,就是创建了一个新的 TimerTargetSelector 对象,并且对其赋值 还加到了 定时器列表里。
这里注意,调用了 timer->release() 减少了一次引用,不会造成timer被释放。看一下ccArrayAppendObject方法后, 知道里面已经对 timer进行了一次retain操作所以 调用了一次release后保证 timer的引用计数为1。


看过这个方法,我们清楚了几点:

  1. tHashTimerEntry 这个结构体是用来记录一个Ref 对象的所有加载的定时器

  2. _hashForTimers 是用来记录所有的 tHashTimerEntry 的链表头指针。

  1. void Scheduler::schedule(SEL_SCHEDULE selector,bool paused)
  2. {
  3. this->schedule(selector,CC_REPEAT_FOREVER,0.0f,paused);
  4. }

这个重载版本和上一个基本类似,不同的只是设置它的执行次数为永久执行。


接着看另一个重载版本:

  1. void Scheduler::schedule(const ccSchedulerFunc& callback,bool paused,const std::string& key)
  2. {
  3. CCASSERT(target,"Argument target must be non-nullptr");
  4. CCASSERT(!key.empty(),"key should not be empty!");
  5.  
  6. tHashTimerEntry *element = nullptr;
  7. HASH_FIND_PTR(_hashForTimers,element);
  8.  
  9. if (! element)
  10. {
  11. element = (tHashTimerEntry *)calloc(sizeof(*element),1);
  12. element->target = target;
  13.  
  14. HASH_ADD_PTR(_hashForTimers,element);
  15.  
  16. // Is this the 1st element ? Then set the pause level to all the selectors of this target
  17. element->paused = paused;
  18. }
  19. else
  20. {
  21. CCASSERT(element->paused == paused,"");
  22. }
  23.  
  24. if (element->timers == nullptr)
  25. {
  26. element->timers = ccArrayNew(10);
  27. }
  28. else
  29. {
  30. for (int i = 0; i < element->timers->num; ++i)
  31. {
  32. TimerTargetCallback *timer = dynamic_cast<TimerTargetCallback*>(element->timers->arr[i]);
  33.  
  34. if (timer && key == timer->getKey())
  35. {
  36. CCLOG("CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: %.4f to %.4f",interval);
  37. timer->setInterval(interval);
  38. return;
  39. }
  40. }
  41. ccArrayEnsureExtraCapacity(element->timers,1);
  42. }
  43.  
  44. TimerTargetCallback *timer = new (std::nothrow) TimerTargetCallback();
  45. timer->initWithCallback(this,callback,key,timer);
  46. timer->release();
  47. }

这个重载版本跟上个基类相同,只是它使用的是void* target,上个重载版本使用的是Ref*版本, 因此这个重载版本可以自定义调度器,因此才使用了TimerTargetCallback。

小结:

Ref和非Ref类型对象的定时器处理基本一样,都加到了调度控制器的_hashFroTimers链表里;

调用schedule方法将指定的对象与回调函数做为参数加到schedule定时器列表里面,加入时会判断是否重复添加


接下来看看几种unschedule方法 ,unschedule将定时器从管理链表里移除:

  1. void Scheduler::unschedule(SEL_SCHEDULE selector,Ref *target)
  2. {
  3. // explicity handle nil arguments when removing an object
  4. if (target == nullptr || selector == nullptr)
  5. {
  6. return;
  7. }
  8. //CCASSERT(target);
  9. //CCASSERT(selector);
  10. tHashTimerEntry *element = nullptr;
  11. HASH_FIND_PTR(_hashForTimers,element);
  12. if (element)
  13. {
  14. for (int i = 0; i < element->timers->num; ++i)
  15. {
  16. TimerTargetSelector *timer = static_cast<TimerTargetSelector*>(element->timers->arr[i]);
  17. if (selector == timer->getSelector())
  18. {
  19. if (timer == element->currentTimer && (! element->currentTimerSalvaged))
  20. {
  21. element->currentTimer->retain();
  22. element->currentTimerSalvaged = true;
  23. }
  24. ccArrayRemoveObjectAtIndex(element->timers,i,true);
  25. // update timerIndex in case we are in tick:,looping over the actions
  26. if (element->timerIndex >= i)
  27. {
  28. element->timerIndex--;
  29. }
  30. if (element->timers->num == 0)
  31. {
  32. if (_currentTarget == element)
  33. {
  34. _currentTargetSalvaged = true;
  35. }
  36. else
  37. {
  38. removeHashElement(element);
  39. }
  40. }
  41. return;
  42. }
  43. }
  44. }
  45. }
根据函数过程来看看,是怎么卸载定时器的: @H_404_104@

  • 参数为一个回调函数指针和一个Ref 对象指针。

  • 在 对象定时器列表_hashForTimers里找是否有 target 对象

  • 在找到了target对象的条件下,对target装载的timers进行逐一遍历

  • 遍历过程 比较当前遍历到的定时器的 selector是等于传入的 selctor

  • 将找到的定时器从element->timers里删除。重新设置timers列表里的 计时器的个数。

  • 最后_currentTarget 与 element的比较值来决定是否从_hashForTimers 将其删除

这些代码过程还是很好理解的,不过程小鱼在看这几行代码的时候有一个问题还没看明白,就是用到了_currentTarget 与 _currentTargetSalvaged 这两个变量,它们的作用是什么呢?下面我们带着这个问题来找答案。


再看另一个unschedule重载版本,基本都是大同小异,都是执行了这几个步骤,只是查找的参数从 selector变成了 std::string &key 对象从 Ref类型变成了void*类型。


现在来看一下update实现,每帧都会调用函数,它是引擎驱动的”灵魂“:
  1. void Scheduler::update(float dt)
  2. {
  3. _updateHashLocked = true;// 这里加了一个状态锁,应该是线程同步的作用。
  4. if (_timeScale != 1.0f)
  5. {
  6. dt *= _timeScale;// 时间速率调整,根据设置的_timeScale 进行了乘法运算。
  7. }
  8. //
  9. // Selector callbacks
  10. //
  11. // 定义了两个链表遍历的指针。
  12. tListEntry *entry,*tmp;
  13. // 处理优先级小于0的定时器,这些定时器存在了_updatesNegList链表里面,具体怎么存进来的,在后面继续分析
  14. DL_FOREACH_SAFE(_updatesNegList,entry,tmp)
  15. {
  16. if ((! entry->paused) && (! entry->markedForDeletion))
  17. {
  18. entry->callback(dt);// 对活动有效的定时器执行回调。
  19. }
  20. }
  21. // 处理优先级为0的定时器。
  22. DL_FOREACH_SAFE(_updates0List,tmp)
  23. {
  24. if ((! entry->paused) && (! entry->markedForDeletion))
  25. {
  26. entry->callback(dt);
  27. }
  28. }
  29. // 处理优先级大于0的定时器
  30. DL_FOREACH_SAFE(_updatesPosList,tmp)
  31. {
  32. if ((! entry->paused) && (! entry->markedForDeletion))
  33. {
  34. entry->callback(dt);
  35. }
  36. }
  37. // 遍历_hashForTimers里自定义的计时器对象列表
  38. for (tHashTimerEntry *elt = _hashForTimers; elt != nullptr; )
  39. {
  40. _currentTarget = elt;// 这里通过遍历动态设置了当前_currentTarget对象。
  41. _currentTargetSalvaged = false;// 当前目标定时器没有被处理过标记
  42. if (! _currentTarget->paused)
  43. {
  44. // 遍历每一个对象的定时器列表
  45. for (elt->timerIndex = 0; elt->timerIndex < elt->timers->num; ++(elt->timerIndex))
  46. {
  47. elt->currentTimer = (Timer*)(elt->timers->arr[elt->timerIndex]);// 这里更新了对象的currentTimer
  48. elt->currentTimerSalvaged = false;
  49. elt->currentTimer->update(dt);// 执行定时器过程。
  50. if (elt->currentTimerSalvaged)
  51. {
  52. // The currentTimer told the remove itself. To prevent the timer from
  53. // accidentally deallocating itself before finishing its step,we retained
  54. // it. Now that step is done,it's safe to release it.
  55. // currentTimerSalvaged的作用是标记当前这个定时器是否已经失效,在设置失效的时候我们对定时器增加过一次引用记数,这里调用release来减少那次引用记数,这样释放很安全,这里用到了这个小技巧,延迟释放,这样后面的程序不会出现非法引用定时器指针而出现错误
  56. elt->currentTimer->release();
  57. }
  58. // currentTimer指针使用完了,设置成空指针
  59. elt->currentTimer = nullptr;
  60. }
  61. }
  62. // elt,at this moment,is still valid
  63. // so it is safe to ask this here (issue #490)
  64. // 因为下面有可能要清除这个对象currentTarget为了循环进行下去,这里先在currentTarget对象还存活的状态下找到链表的下一个指针。
  65. elt = (tHashTimerEntry *)elt->hh.next;
  66. // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
  67. // 如果_currentTartetSalvaged 为 true 且这个对象里面的定时器列表为空那么这个对象就没有计时任务了我们要把它从__hashForTimers列表里面删除
  68. if (_currentTargetSalvaged && _currentTarget->timers->num == 0)
  69. {
  70. removeHashElement(_currentTarget);
  71. }
  72. }
  73. // 下面这三个循环也是清理工作
  74. // updates with priority < 0
  75. DL_FOREACH_SAFE(_updatesNegList,tmp)
  76. {
  77. if (entry->markedForDeletion)
  78. {
  79. this->removeUpdateFromHash(entry);
  80. }
  81. }
  82. // updates with priority == 0
  83. DL_FOREACH_SAFE(_updates0List,tmp)
  84. {
  85. if (entry->markedForDeletion)
  86. {
  87. this->removeUpdateFromHash(entry);
  88. }
  89. }
  90. // updates with priority > 0
  91. DL_FOREACH_SAFE(_updatesPosList,tmp)
  92. {
  93. if (entry->markedForDeletion)
  94. {
  95. this->removeUpdateFromHash(entry);
  96. }
  97. }
  98. _updateHashLocked = false;
  99. _currentTarget = nullptr;
  100. #if CC_ENABLE_SCRIPT_BINDING
  101. //
  102. // Script callbacks
  103. //
  104. // Iterate over all the script callbacks
  105. if (!_scriptHandlerEntries.empty())
  106. {
  107. for (auto i = _scriptHandlerEntries.size() - 1; i >= 0; i--)
  108. {
  109. SchedulerScriptHandlerEntry* eachEntry = _scriptHandlerEntries.at(i);
  110. if (eachEntry->isMarkedForDeletion())
  111. {
  112. _scriptHandlerEntries.erase(i);
  113. }
  114. else if (!eachEntry->isPaused())
  115. {
  116. eachEntry->getTimer()->update(dt);
  117. }
  118. }
  119. }
  120. #endif
  121. //
  122. // 上面都是对象的定时任务,这里是多线程处理函数的定时任务。
  123. //
  124. // Testing size is faster than locking / unlocking.
  125. // And almost never there will be functions scheduled to be called. 这块作者已经说明了,函数的定时任务不常用。我们简单了解一下就可了。
  126. if( !_functionsToPerform.empty() ) {
  127. _performMutex.lock();
  128. // fixed #4123: Save the callback functions,they must be invoked after '_performMutex.unlock()',otherwise if new functions are added in callback,it will cause thread deadlock.
  129. auto temp = _functionsToPerform;
  130. _functionsToPerform.clear();
  131. _performMutex.unlock();
  132. for( const auto &function : temp ) {
  133. function();
  134. }
  135. }
  136. }

在整个函数中:

_currentTarget实在update主循环过程中用来标记当前执行到哪个target对象;

_currentTargetSalvaged是标记_currentTarget是否需要进行清除操作的变量。


在Scheduler中有一个scheduleUpdate函数,什么时候调用这个呢,帧帧调用时会调用这个,来看看Node中的两个函数

  1. void scheduleUpdate(void);
  2. void scheduleUpdateWithPriority(int priority);

在Node定义默认都是0级别的结点,这两个函数最后还是调用了Scheduler中的scheduleUpdate函数
  1. void scheduleUpdate(T *target,int priority,bool paused)
  2. {
  3. this->schedulePerFrame([target](float dt){
  4. target->update(dt);
  5. },priority,paused);
  6. }

然后来看看schedulePerFrame函数都做了些什么:
  1. void Scheduler::schedulePerFrame(const ccSchedulerFunc& callback,bool paused)
  2. {
  3. tHashUpdateEntry *hashElement = nullptr;
  4. HASH_FIND_PTR(_hashForUpdates,hashElement);
  5. if (hashElement)
  6. {
  7. // check if priority has changed
  8. if ((*hashElement->list)->priority != priority)
  9. {
  10. if (_updateHashLocked)
  11. {
  12. CCLOG("warning: you CANNOT change update priority in scheduled function");
  13. hashElement->entry->markedForDeletion = false;
  14. hashElement->entry->paused = paused;
  15. return;
  16. }
  17. else
  18. {
  19. // will be added again outside if (hashElement).
  20. unscheduleUpdate(target);
  21. }
  22. }
  23. else
  24. {
  25. hashElement->entry->markedForDeletion = false;
  26. hashElement->entry->paused = paused;
  27. return;
  28. }
  29. }
  30.  
  31. // most of the updates are going to be 0,that's way there
  32. // is an special list for updates with priority 0
  33. if (priority == 0)
  34. {
  35. appendIn(&_updates0List,paused);
  36. }
  37. else if (priority < 0)
  38. {
  39. priorityIn(&_updatesNegList,paused);
  40. }
  41. else
  42. {
  43. // priority > 0
  44. priorityIn(&_updatesPosList,paused);
  45. }
  46. }

在这里可以看出将调度器添加到相应权限的列表中。


Scheduler类在官方文档的帮助下就分析了这么多,但是我知道我还是初步了解了调度器的工作原理,以后会更加努力的!

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