Cocos2D-x schedule & scheduleUpdate 的使用
Cocos2D-x schedule & scheduleUpdate 的使用
要取消这个定时器调用也很简单。
参数中指定了selector和执行时间间隔。
③
参数中指定了selector和开始执行前的延时。
④
这个方法中的参数只指定了selector,那么它的作用其实就等同于scheduleUpdate,即节点在每一帧都会执行selector方法。
取消所有的定时器方法。
开始学习Cocos2D-x
在cocos2d-x中提供了好几个定时器的方法供调用我们可以在CCNode.h 这个头文件中找到相应的方法,下面整理一下:
(1)使用下面这个方法,那么节点在每一帧都会执行update方法。
- /**
- * Schedules the "update" method.
- *
- * It will use the order number 0. This method will be called every frame.
- * 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.
- */
- void scheduleUpdate(void);
在注释中说到,该方法的order(优先级数)为0,order(优先级数)越小那么越先调度。每一个节点都只能有一个update调度方法。
注意要重写该update方法。
- /*
- * Update method will be called automatically every frame if "scheduleUpdate" is called, and the node is "live"
- */
- virtual void update(float delta);
要取消这个定时器调用也很简单。
- /*
- * Unschedules the "update" method.
- * @see scheduleUpdate();
- */
- void unscheduleUpdate(void);
cocos2d-x中还提供了这样一个方法,这个方法的作用和scheduleUpdate类似,只不过还有指定一个优先级,优先级数越小,那么越先执行。
- /**
- * Schedules the "update" method with a custom priority.
- *
- * 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).
- */
- void scheduleUpdateWithPriority(int priority);
(2)cocos2d-x中还提供了好几个schedule方法。这些方法的调用都可以指定节点执行特定的selector。
①
- /**
- * 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 interval Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead.
- * @param repeat The selector will be excuted (repeat + 1) times, you can use kCCRepeatForever for tick infinitely.
- * @param delay The amount of time that the first tick will wait before execution.
- */
- void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
参数中指定了selector,执行时间间隔,重复执行次数(可以使用kCCRepeatForever表示无限循环) ,开始执行前的延时。
- /**
- * Schedules a custom selector with an interval time in seconds.
- * @see schedule(SEL_SCHEDULE, float, unsigned int, float)
- *
- * @param selector A function wrapped as a selector
- * @param interval Callback interval time in seconds. 0 means tick every frame,
- */
- void schedule(SEL_SCHEDULE selector, float interval);
参数中指定了selector和执行时间间隔。
③
- /**
- * Schedules a selector that runs only once, with a delay of 0 or larger
- * @see schedule(SEL_SCHEDULE, float, unsigned int, float)
- *
- * @param selector A function wrapped as a selector
- * @param delay The amount of time that the first tick will wait before execution.
- */
- void scheduleOnce(SEL_SCHEDULE selector, float delay);
参数中指定了selector和开始执行前的延时。
④
- /**
- * 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
- */
- void schedule(SEL_SCHEDULE selector);
这个方法中的参数只指定了selector,那么它的作用其实就等同于scheduleUpdate,即节点在每一帧都会执行selector方法。
⑤ 取消定时器schedule的方法
- /**
- * Unschedules a custom selector.
- * @see schedule(SEL_SCHEDULE, float, unsigned int, float)
- *
- * @param selector A function wrapped as a selector
- */
- void unschedule(SEL_SCHEDULE selector);
取消所有的定时器方法。
- /**
- * Unschedule all scheduled selectors: custom selectors, and the 'update' selector.
- * Actions are not affected by this method.
- */
- void unscheduleAllSelectors(void);
⑥ 下面的两个方法是用于恢复和暂停定时器方法的。他们一般都是在onEnter和onExit方法种调用的。
- /**
- * Resumes all scheduled selectors and actions.
- * This method is called internally by onEnter
- */
- void resumeSchedulerAndActions(void);
- /**
- * Pauses all scheduled selectors and actions.
- * This method is called internally by onExit
- */
- void pauseSchedulerAndActions(void);