scheduleUpdate和unscheduleUpdate

做坦克练习时,子弹和坦克碰撞后  一起消失。但是再次发射子弹时候就挂了。。。

Assertion failed: (hashElement->entry->markedForDeletion), function schedulePerFrame, file /Users/tof_/Documents/program/LinksOnline/cocos2d/cocos/2d/CCScheduler.cpp, line 470


子弹坦克碰撞后没有调用unscheduleUpdate();

再次发射子弹时,又调用scheduleUpdate();    导致调用两次scheduleUpdate(); 所以报错。  

为什么不un,再次调用会报错。从源码上暂时没看明白

------------------------------------------------------------------------------------------------------------------------------------------------

http://blog.csdn.net/summerhust/article/details/17239481   网查时看到别人对计时器的总结

在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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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);  


猜你喜欢

转载自blog.csdn.net/ActionReaction/article/details/27705659