Cocos2D-x schedule & scheduleUpdate 的使用

來源:互聯網
上載者:User

開始學習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,執行時間間隔,是否重複執行 ,開始執行前的延時。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.