Draw and update in cocos2d and cocos2d-X

Source: Internet
Author: User

Like other game engines, we have two different ways to complete draw and update.

1: Draw: Each CCNode has a draw method, and each frame is called. We only do what is depicted in this method.

2: Update: by default, CCNode does not have update. If you want to Update the status, you can register a callback function in the following four ways:

ScheduleUpdate:

ScheduleUpdateWithPriority:

ScheduleSelector :( SEL) selector

ScheduleSelector: (SEL) selector interval :( float) interval

The registration method is called only when the current scenario is active and there is no Pause. We recommend that you use scheduleUpdate or scheduleUpdateWithPriority. Because it runs faster and consumes less memory. For example:

 

-(Id) init
{
//-10 indicates the priority. The smaller the value, the higher the priority.
[Self scheduleUpdateWithPriority:-10];
}
-(Void) update :( ccTime) deltaTime
{
// Update your node here
// DON't draw it, JUST update it.
// Example:
Rotation _ = value * deltaTime;
}
 
After scheduleUpdate or scheduleUpdateWithPriority is called, the system will find the update method we have defined, which is equivalent to schedule's update method. The update method for removing registration can be:

[Node unscheduleUpdate];
We can also use scheduleSelector, which is an old method with no priority. If no interval exists or if interval = 0, each frame is called.

-(Id) init
{
// The "step:" selector will be called every 0.5 seconds
[Self schedule: @ selector (step :) interval: 0.5f]
}
 
-(Void) step :( ccTime) deltaTime
{
// Update your stuff here
}
Cancel this method:

[Node unscheduleSelector: @ selector (step :)];

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.