Cocos2d-x 3.0 schedule in node

Source: Internet
Author: User

* *********************************** Please specify the source: bytes ********************************************


Aha ~, After that, I started to prepare for some real games ~

It is very important to build the foundation. If the foundation is ready, you can start other things ~ \ (Too many rows )/~ La la



The task of this study is scheduled. As the name suggests, it does not need to be implemented by itself.

The most common is the dynamic background in some of the startup interfaces of many mobile games.


In the last study, I learned the screen touch and message distribution mechanisms,

Finally, we touch the screen, and the genie will move to the position touched.

This time, the goal is to let the role itself move from left to right (repeatedly ).


In the cocos2d-x, there is a large loop that refresh the screen at 60 frames per second,

To join our own scheduled tasks, we need to use this frequency to add our own tasks to this large loop.

First, develop your own update function,

Add the UPDATE function in helloworldscene. h:

// for schedulevoid update( float t );



Then, define the action in. cpp,

Our action is to move the genie from left to right, that is, use setposition,

When moving repeatedly, a judgment statement is added. If the boundary is greater than the right side, the moving starts from the leftmost side.

Void helloworld: Update (float t) {auto sprite = This-> getchildbytag (33); sprite-> setpositionx (sprite-> getpositionx () + 1 ); // you can see how much the screen width is ~ If (sprite-> getpositionx ()> 960) sprite-> setpositionx (0 );}



Finally, don't forget to add scheduled tasks to the init function ~

// Add a scheduled task this-> scheduleupdate (); // Add an update task



In this way, you can find that,

You don't need to touch the screen on your own. You can move your genie from the middle to the right, and then from the left to the right,

Why is it moving from the center to the right?

Bug ?! No !!!

Because we set the genie, the initial position is in the middle ~


OK. You can find that our frequency is fixed. Can we control the speed of moving?

Of course!


Method 1:

To set a counting variable,

We can define a counting variable Js in the class,

Then, every time the UPDATE function is called, JS ++

If JS <10 is returned, the following statement is not executed,

When JS = 10, you can continue the execution and finally let JS be 0.

Through the above control, we can reduce the speed of 60 per second to 6 per second:

(Not only declare JS variables in. H files, but also initialize to 0 in the init function)

Void helloworld: Update (float t) {++ JS; If (JS <10) return; Auto sprite = This-> getchildbytag (33 ); sprite-> setpositionx (sprite-> getpositionx () + 1); // you can see how much the screen width is ~ If (sprite-> getpositionx ()> 960) sprite-> setpositionx (0); JS = 0 ;}


Method 2:

Built-in method:

Schedule (schedule_selector (_ selector), float time );

The first parameter is the function to call, and the second parameter is the call interval.

this->schedule(schedule_selector(HelloWorld::update),1);

Void helloworld: Update (float t) {auto sprite = This-> getchildbytag (33); sprite-> setpositionx (sprite-> getpositionx () + 1 ); // you can see how much the screen width is ~ If (sprite-> getpositionx ()> 960) sprite-> setpositionx (0 );}

In this way, you can call the UPDATE function once every one second. Of course, you can also call another function.



And, there are other options for scheduler tasks:

-- Start

-- Pause

-- Stop

-- Restore

These in the test program provided by the cocos2d-x has detailed use method, here will not talk about it first ~


Well, today, that's it!

(Turn off the lights ~ Second (second 3 rows) second)



* *********************************** Please specify the source: bytes ********************************************

Cocos2d-x 3.0 schedule in node

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.