Cocos2D-x schedule & scheduleUpdate 的使用

Cocos2D-x  schedule & scheduleUpdate 的使用              
 


开始学习Cocos2D-x


在cocos2d-x中提供了好几个定时器的方法供调用我们可以在CCNode.h 这个头文件中找到相应的方法,下面整理一下:


(1)使用下面这个方法,那么节点在每一帧都会执行update方法。


  1. /**  
  2.      * Schedules the "update" method.  
  3.      * 
  4.      * It will use the order number 0. This method will be called every frame. 
  5.      * Scheduled methods with a lower order value will be called before the ones that have a higher order value. 
  6.      * Only one "update" method could be scheduled per node. 
  7.      */  
  8.     void scheduleUpdate(void);  


在注释中说到,该方法的order(优先级数)为0,order(优先级数)越小那么越先调度。每一个节点都只能有一个update调度方法。


注意要重写该update方法。


  1. /*  
  2.      * Update method will be called automatically every frame if "scheduleUpdate" is called, and the node is "live" 
  3.      */  
  4.     virtual void update(float delta);  

要取消这个定时器调用也很简单。


  1. /*  
  2.      * Unschedules the "update" method. 
  3.      * @see scheduleUpdate(); 
  4.      */  
  5.     void unscheduleUpdate(void);  


cocos2d-x中还提供了这样一个方法,这个方法的作用和scheduleUpdate类似,只不过还有指定一个优先级,优先级数越小,那么越先执行。


  1. /**  
  2.      * Schedules the "update" method with a custom priority.  
  3.      * 
  4.      * This selector will be called every frame. 
  5.      * Scheduled methods with a lower priority will be called before the ones that have a higher value. 
  6.      * Only one "update" selector could be scheduled per node (You can't have 2 'update' selectors). 
  7.      */  
  8.     void scheduleUpdateWithPriority(int priority);  


(2)cocos2d-x中还提供了好几个schedule方法。这些方法的调用都可以指定节点执行特定的selector。


  1. /** 
  2.      * Schedules a custom selector. 
  3.      * 
  4.      * If the selector is already scheduled, then the interval parameter will be updated without scheduling it again. 
  5.      * @code 
  6.      * // firstly, implement a schedule function 
  7.      * void MyNode::TickMe(float dt); 
  8.      * // wrap this function into a selector via schedule_selector marco. 
  9.      * this->schedule(schedule_selector(MyNode::TickMe), 0, 0, 0); 
  10.      * @endcode 
  11.      * 
  12.      * @param interval  Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead. 
  13.      * @param repeat    The selector will be excuted (repeat + 1) times, you can use kCCRepeatForever for tick infinitely. 
  14.      * @param delay     The amount of time that the first tick will wait before execution. 
  15.      */  
  16.     void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);  


参数中指定了selector,执行时间间隔,重复执行次数(可以使用kCCRepeatForever表示无限循环) ,开始执行前的延时。



  1. /** 
  2.      * Schedules a custom selector with an interval time in seconds. 
  3.      * @see schedule(SEL_SCHEDULE, float, unsigned int, float) 
  4.      * 
  5.      * @param selector      A function wrapped as a selector 
  6.      * @param interval      Callback interval time in seconds. 0 means tick every frame, 
  7.      */  
  8.     void schedule(SEL_SCHEDULE selector, float interval);  

参数中指定了selector和执行时间间隔。


  1. /** 
  2.      * Schedules a selector that runs only once, with a delay of 0 or larger 
  3.      * @see schedule(SEL_SCHEDULE, float, unsigned int, float) 
  4.      * 
  5.      * @param selector      A function wrapped as a selector 
  6.      * @param delay         The amount of time that the first tick will wait before execution. 
  7.      */  
  8.     void scheduleOnce(SEL_SCHEDULE selector, float delay);  

参数中指定了selector和开始执行前的延时。


  1. /** 
  2.     * Schedules a custom selector, the scheduled selector will be ticked every frame 
  3.     * @see schedule(SEL_SCHEDULE, float, unsigned int, float) 
  4.     * 
  5.     * @param selector      A function wrapped as a selector 
  6.     */  
  7.    void schedule(SEL_SCHEDULE selector);  

这个方法中的参数只指定了selector,那么它的作用其实就等同于scheduleUpdate,即节点在每一帧都会执行selector方法。


⑤ 取消定时器schedule的方法


  1. /**  
  2.      * Unschedules a custom selector. 
  3.      * @see schedule(SEL_SCHEDULE, float, unsigned int, float) 
  4.      * 
  5.      * @param selector      A function wrapped as a selector 
  6.      */  
  7.     void unschedule(SEL_SCHEDULE selector);  

取消所有的定时器方法。


  1. /**  
  2.      * Unschedule all scheduled selectors: custom selectors, and the 'update' selector. 
  3.      * Actions are not affected by this method. 
  4.      */  
  5.     void unscheduleAllSelectors(void);  


⑥ 下面的两个方法是用于恢复和暂停定时器方法的。他们一般都是在onEnter和onExit方法种调用的。


  1. /**  
  2.      * Resumes all scheduled selectors and actions. 
  3.      * This method is called internally by onEnter 
  4.      */  
  5.     void resumeSchedulerAndActions(void);  
  6.     /**  
  7.      * Pauses all scheduled selectors and actions. 
  8.      * This method is called internally by onExit 
  9.      */  
  10.     void pauseSchedulerAndActions(void);  
原文地址:https://www.cnblogs.com/xiayong123/p/3717339.html