Cocos2dx BASICS (8) -- timer updates schedule and update

Source: Internet
Author: User
Tags addchild


[Content of this section]

The timer is indispensable in most games, that is, the corresponding refresh body function should be executed at intervals to update the screen, time, progress, and enemy commands of the game.

Cocos2dx provides related operations for timer schedule. The operation functions are defined in ccnode. Therefore, most engine classes can Set timers, such as cclayer, ccsprite, and ccmenu.


There are three types of timer update Methods:

(1) default Timer: scheduleupdate ();

(2) custom Timer: Schedule ();

(3) One-time Timer: scheduleonce ();



[1. scheduleupdate]

Default Timer: scheduleupdate ().

The default number of refresh times of the timer is related to the screen refresh frequency. If the frequency is 60 frames per second, scheduleupdate performs 60 refreshes per second.

The refresh function body corresponding to scheduleupdate is Update (), that is, each frame executes the update () function.

Perform the following operations:

// Enable the default timer. Refresh Interval is one frame. Void scheduleupdate (); void scheduleupdatewithpriority (INT priority); // give priority. The smaller the priority, the higher the priority virtual void Update (float delta); // update is the refresh function body of the scheduleupdate timer.



Ii. Schedule]

Custom Timer: Schedule ().

The timer can customize the specified function body, the number of times the function body is refreshed, the refresh frequency, and the start time.

The function body is not defined as update () and can be customized.

Perform the following operations:

// Set the custom timer. By default, the refresh interval is one frame. // Interval: run every interval second. // Repeat: number of repetitions. // Delay: delay time, that is, refresh starts after the timer delay is created. // Schedule (schedule_selector (helloworld: myupdate), 1.0/60.0); void schedule (sel_schedule selector); // The default refresh interval is one frame void schedule (sel_schedule selector, float interval); // custom refresh interval. Unit: Second void schedule (sel_schedule selector, float interval, unsigned int repeat, float delay );



Iii. scheduleonce]

One-time Timer: scheduleonce ().

After waiting for the delay second delay time, the timer only refreshes the function body once and does not refresh again.

Perform the following operations:

// Only run once, and run the command after delay in seconds // scheduleonce (schedule_selector (helloworld: myupdate), 5.0); void scheduleonce (sel_schedule selector, float delay );



[4. Other operations]

Cancel, pause, and resume the timer.

Perform the following operations:

// This-> unscheduleupdate (); // sprite-> unscheduleallselectors (); void unscheduleupdate (void); // cancel the default timer void unschedule (sel_schedule selector ); // cancel the User-Defined Function timer void unscheduleallselectors (void); // cancel all timer void pauseschedulerandactions (void); // pause all timer and Action Void resumeschedulerandactions (void ); // restore all timer and action



[Code practice]

(1) Create five genie in helloworld: Init (), which correspond to the five timer-defining methods one by one.

// Create five genie ccsprite * sp = ccsprite: Create ("icon.png"); SP-> setposition (CCP (30, mysize. height-30); this-> addchild (SP, 0,100); // tag 100ccsprite * SP1 = ccsprite: Create ("icon.png "); SP1-> setposition (CCP (30, mysize. height-90); this-> addchild (SP1, 0,101); // tag 101ccsprite * SP2 = ccsprite: Create ("icon.png "); SP2-> setposition (CCP (30, mysize. height-150); this-> addchild (SP2, 0,102); // tag to mark 102ccsprite * SP3 = ccsprite: Create ("icon.png "); SP3-> setposition (CCP (30, mysize. height-210); this-> addchild (SP3, 0,103); // tag to mark 103ccsprite * SP4 = ccsprite: Create ("icon.png "); SP4-> setposition (CCP (30, mysize. height-270); this-> addchild (SP4, 0,104); // tag 104 // define five timers, update the Genie this-> scheduleupdate (); this-> schedule (schedule_selector (helloworld: myupdate); this-> schedule (partition (helloworld: myupdate2), 1.0f); this-> schedule (schedule_selector (helloworld :: myupdate3), 1.0f, 5, 3.0f); this-> scheduleonce (schedule_selector (helloworld: myupdate4), 5.0f );//

(2) Compile the refresh function body corresponding to the Timer:

// Scheduleupdatevoid helloworld: Update (float DT) {ccsprite * sp = (ccsprite *) This-> getchildbytag( 100 ); // get the sprite SP-> setposition (SP-> getposition () + CCP (100) of tag =; // move 1} // schedule (schedule_selector) void helloworld: myupdate (float DT) {ccsprite * SP1 = (ccsprite *) This-> getchildbytag( 101 ); // get the sprite SP1-> setposition (SP1-> getposition () + CCP (101) of tag =; // move 1} // schedule (schedule_selector, interval) void helloworld: myupdate2 (float DT) {ccsprite * SP2 = (ccsprite *) This-> getchildbytag( 102 ); // get the sprite SP2 with Tag = 102-> setposition (SP2-> getposition () + CCP (60, 0); // move 60} // schedule (schedule_selector, interval, repeat, delay) void helloworld: myupdate3 (float DT) {ccsprite * SP3 = (ccsprite *) This-> getchildbytag (103 ); // get the genie SP3-> setposition (SP3-> getposition () + CCP (60, 0) for tag = 103; // move 60} // scheduleoncevoid helloworld :: myupdate4 (float DT) {ccsprite * SP4 = (ccsprite *) This-> getchildbytag (104 ); // get the sprite SP4 with Tag = 104-> setposition (SP4-> getposition () + CCP (100); // move }//

Running result:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/46/8D/wKioL1Py58PgszrlAA4rf6Q3Yv8996.gif "Title =" 2.gif" alt = "wkiol1py58pgszrlaa4rf6q3yv8996.gif"/>

Analyze the running result:

(1) scheduleupdate () and Schedule (schedule_selector) have the same effect, but schedule can customize the refresh function body, not necessarily Update (). The refresh function body of scheduleupdate () can only be update ().

(2) Schedule (schedule_selector, interval): interval = 1.0 is set, so myupdate2 () is executed every 1.0 seconds ().

(3) Schedule (schedule_selector, interval, repeat, delay): myupdate3 () is executed only after 3.0f seconds. After that, the update is stopped after repeated 5 times.

(4) scheduleonce (schedule_selector): After 5 seconds, the update is stopped after only one myupdate4 () operation is executed.



[Demo download]

Http://down.51cto.com/data/1864929



This article is from the "summer wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1542014

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.