Cocos2D-x schedule & scheduleUpdate 的使用

本文介绍了Cocos2D-x中定时器的使用方法,包括scheduleUpdate和schedule,详细解释了如何使用这些方法实现定时执行特定函数,并提供了取消定时器的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);  
/** 
     * 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方法。


  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);  
/* 
     * Update method will be called automatically every frame if "scheduleUpdate" is called, and the node is "live"
     */
    virtual void update(float delta);

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


  1. /*  
  2.      * Unschedules the "update" method. 
  3.      * @see scheduleUpdate(); 
  4.      */  
  5.     void unscheduleUpdate(void);  
/* 
     * Unschedules the "update" method.
     * @see scheduleUpdate();
     */
    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);  
/** 
     * 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。


  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);  
/**
     * 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表示无限循环) ,开始执行前的延时。



  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);  
/**
     * 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和执行时间间隔。


  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);  
/**
     * 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和开始执行前的延时。


  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);  
 /**
     * 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的方法


  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);  
/** 
     * 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);

取消所有的定时器方法。


  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);  
/** 
     * Unschedule all scheduled selectors: custom selectors, and the 'update' selector.
     * Actions are not affected by this method.
     */
    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);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值