cocos2dx中的调度器
2015-08-06 11:03
351 查看
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类中提供了调度器相关的函数。
默认调度器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);方法来停止该调度器。
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);方法来停止该调度器。
相关文章推荐
- Cocos2d-x中背景音乐和音效使用实例
- Cocos2d-x学习笔记之CCScene、CCLayer、CCSprite的默认坐标和默认锚点实验
- Cocos2d-x UI开发之CCControlPotentiometer控件类使用实例
- Cocos2d-x UI开发之文本类使用实例
- Cocos2d-x保存用户游戏数据之XML文件是否存在问题判断方法
- Cocos2d-x UI开发之菜单类使用实例
- Cocos2d-x UI开发之CCControlButton控件类实例
- Cocos2d-x学习笔记之世界坐标系、本地坐标系、opengl坐标系、屏幕坐标系
- Cocos2d-x UI开发之CCControlColourPicker控件类使用实例
- Cocos2d-x学习笔记之CCLayerColor层的使用实例
- Cocos2d-x Schedule定时器的使用实例
- Cocos2d-x中使用CCScrollView来实现关卡选择实例
- Cocos2d-x中CCEditBox文本输入框的使用实例
- cocos2d-x学习笔记之CCLayer、CCLayerColor、CCLayerGradient、CCLayerMultiplex场景层介绍
- Cocos2d-x UI开发之CCControlSlider控件类使用实例
- Cocos2d-x学习笔记之开发环境搭建
- Cocos2d-x学习笔记之Hello World!
- Cocos2d-x的内存管理总结
- Cocos2d-x 3.0中集成社交分享ShareSDK的详细步骤和常见问题解决
- Cocos2d-x入门教程(详细的实例和讲解)