cocos2dx中的调度器
cocos2dx提供了多种调度器,通常我们使用其中常见的3种:
- 默认调度器:void scheduleUpdate(void);
- 自定义调度器:void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
- 单次调度器:void scheduleOnce(SEL_SCHEDULE selector, float delay);
CCNode类中提供了调度器相关的函数。
* Sets a Scheduler object that is used to schedule all "updates" and timers. 设置一个调度对象,用来调度所有updates和计时器
*
* @warning If you set a new Scheduler, then previously created timers/update are going to be removed. 警告:如果你设置一个新的调度器,那么以前创建的计数器和update将被删除
* @param scheduler A Shdeduler object that is used to schedule all "update" and timers.
*/
virtual void setScheduler(Scheduler* scheduler);
/**
* Gets a Sheduler object. 获得一个调度器对象
*
* @see setScheduler(Scheduler*)
* @return A Scheduler object.
*/
virtual Scheduler* getScheduler() { return _scheduler; }
virtual const Scheduler* getScheduler() const { return _scheduler; }
/**
* Checks whether a selector is scheduled. 判断调度器是否被调度中
*
* @param selector A function selector
* @return Whether the funcion selector is scheduled.
* @js NA
* @lua NA
*/
bool isScheduled(SEL_SCHEDULE selector);
/**
* Schedules the "update" method. (默认的调度器)调度update方法
*
* It will use the order number 0. This method will be called every frame. 它将使用优先级为0,该方法每帧都会被调用
* Scheduled methods with a lower order value will be called before the ones that have a higher order value. 调度值较小的调度函数会比调度值较高的函数优先被调用
* Only one "update" method could be scheduled per node. 每个节点只能有一个update被调度
* @js NA
* @lua NA
*/
void scheduleUpdate(void);
/**
* Schedules the "update" method with a custom priority. 自定义优先级的调度update方法
*
* This selector will be called every frame. 这个选择器每帧都会被调用
* Scheduled methods with a lower priority will be called before the ones that have a higher value. 调度值较小的调度函数会比调度值较高的函数优先被调用
* Only one "update" selector could be scheduled per node (You can't have 2 'update' selectors).
* @js NA
* @lua NA
*/
void scheduleUpdateWithPriority(int priority);
/*
* Unschedules the "update" method. 停止调度update方法
* @see scheduleUpdate();
*/
void unscheduleUpdate(void);
/**
* Schedules a custom selector. 自定义调度器
*
* If the selector is already scheduled, then the interval parameter will be updated without scheduling it again.
@code
// firstly, implement a schedule function
void MyNode::TickMe(float dt);
// wrap this function into a selector via schedule_selector marco.
this->schedule(schedule_selector(MyNode::TickMe), 0, 0, 0);
@endcode
*
* @param selector The SEL_SCHEDULE selector to be scheduled. 调度函数
* @param interval Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead. 调度时间。0表示每帧都被调度。如果为0,相反应该推荐使用scheduleUpdate
* @param repeat The selector will be excuted (repeat + 1) times, you can use kRepeatForever for tick infinitely. 调度函数将被调度repeat+1次,你可以使用参数kRepeatForever表示永远调用
* @param delay The amount of time that the first tick will wait before execution. 执行第一次调度之前等待的延迟时间
* @lua NA
*/
void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
/**
* Schedules a custom selector with an interval time in seconds. 调度一个有时间间隔的自定义调度器
* @see `schedule(SEL_SCHEDULE, float, unsigned int, float)`
*
* @param selector The SEL_SCHEDULE selector to be scheduled.
* @param interval Callback interval time in seconds. 0 means tick every frame,
* @lua NA
*/
void schedule(SEL_SCHEDULE selector, float interval);
/**
* Schedules a selector that runs only once, with a delay of 0 or larger 调度一个带有延迟时间的单次调度器
* @see `schedule(SEL_SCHEDULE, float, unsigned int, float)`
*
* @param selector The SEL_SCHEDULE selector to be scheduled.
* @param delay The amount of time that the first tick will wait before execution.
* @lua NA
*/
void scheduleOnce(SEL_SCHEDULE selector, float delay);
/**
* Schedules a custom selector, the scheduled selector will be ticked every frame
* @see schedule(SEL_SCHEDULE, float, unsigned int, float)
*
* @param selector A function wrapped as a selector
* @lua NA
*/
void schedule(SEL_SCHEDULE selector);
/**
* Unschedules a custom selector. 停止调度一个自定义的调度器
* @see `schedule(SEL_SCHEDULE, float, unsigned int, float)`
*
* @param selector A function wrapped as a selector
* @lua NA
*/
void unschedule(SEL_SCHEDULE selector);
/**
* Unschedule all scheduled selectors: custom selectors, and the 'update' selector. 停止调度所有调度器:包括自定义调度器和默认调度器update
* Actions are not affected by this method. 这个方法不影响动作
* @lua NA
*/
void unscheduleAllSelectors(void);
/**
* Resumes all scheduled selectors, actions and event listeners. 恢复启动所有调度器,动作和事件监听器
* This method is called internally by onEnter 该方法在内部被onEnter方法调用
*/
void resume(void);
/**
* Pauses all scheduled selectors, actions and event listeners.. 暂停所有调度器,动作和事件监听器
* This method is called internally by onExit 该方法在内部被onExit方法调用
*/
void pause(void);
/**
* Resumes all scheduled selectors, actions and event listeners. 同resume方法,不再推荐使用
* This method is called internally by onEnter
*/
CC_DEPRECATED_ATTRIBUTE void resumeSchedulerAndActions();
/**
* Pauses all scheduled selectors, actions and event listeners.. 同pause方法,不再推荐使用
* This method is called internally by onExit
*/
CC_DEPRECATED_ATTRIBUTE void pauseSchedulerAndActions();
/*
* Update method will be called automatically every frame if "scheduleUpdate" is called, and the node is "live"
*/ //update方法每帧都会被调用,当scheduleUpdate被调用而且节点node还存活的时候
virtual void update(float delta);
默认调度器scheduleUpdate
该方法在内部调用了节点node的update方法,update方法在每帧都会被调用一次。通过重载该update方法,在update方法内部实现我们的逻辑代码。
可以通过 void unscheduleUpdate(void);方法来停止调度该调度器。
自定义调度器schedule
void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
如果要指定间隔多少时间调度一次,可以使用自定义调度器。
不需要该调度器的时候可以通过void unschedule(SEL_SCHEDULE selector);方法来停止该调度器。
单次调度器scheduleOnce
void scheduleOnce(SEL_SCHEDULE selector, float delay);
对于只执行一次调度的时候可以用单次调度器。
同样可以用void unschedule(SEL_SCHEDULE selector);方法来停止该调度器。