開始學習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);