Use of Cocos2D-x schedule & amp; scheduleUpdate

Source: Internet
Author: User

 


Learning Cocos2D-x

 


Several timer methods are provided in the cocos2d-x for calling. We can find the corresponding method in the header file CCNode. h. below:

 


(1) using the following method, the node will execute the update method at each frame.


[Cpp]
/**
* 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 cocould be scheduled per node.
*/
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 cocould be scheduled per node.
*/
Void scheduleUpdate (void );

 

As mentioned in the comment, the order (priority level) of this method is 0, and the smaller the order (priority level), the more advanced scheduling. Each node can have only one update scheduling method.

 


Note that you must override the update method.


[Cpp]
/*
* Update method will be called automatically every frame if "scheduleUpdate" is called, and the node is "live"
*/
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 );
It is easy to cancel the timer call.


[Cpp]
/*
* Unschedules the "update" method.
* @ See scheduleUpdate ();
*/
Void unscheduleUpdate (void );

/*
* Unschedules the "update" method.
* @ See scheduleUpdate ();
*/
Void unscheduleUpdate (void );

 

The cocos2d-x also provides this method, which works like scheduleUpdate, but also specifies a priority, the smaller the priority level, the more first execution.


[Cpp]
/**
* 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 cocould be scheduled per node (You can't have 2 'update' selectors ).
*/
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 cocould be scheduled per node (You can't have 2 'update' selectors ).
*/
Void scheduleUpdateWithPriority (int priority );

 

(2) Several schedule methods are also provided in the cocos2d-x. You can specify a node to execute a specific selector to call these methods.


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

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

 

The selector parameter specifies the execution interval, whether to execute repeatedly, and the delay before execution.

 


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

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

The selector and execution interval are specified in the parameter.


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

/**
* 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 );
The selector parameter specifies the latency before execution.


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

/**
* 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 );
The parameters in this method only specify selector, so its function is equivalent to scheduleUpdate, that is, the node will execute the selector method at each frame.

 


⑤ Method for canceling the schedule timer


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

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

Cancel all timer methods.


[Cpp]
/**
* Unschedule all scheduled selectors: custom selectors, and the 'update' selector.
* Actions are not affected by this method.
*/
Void unscheduleAllSelectors (void );

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

 

⑥ The following two methods are used to recover and pause the timer. They are generally called in onEnter and onExit methods.

 

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

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.