Cocos2d-x Study Notes (21) -- CCSpriteBatchNode

Source: Internet
Author: User

Cocos2d-x Study Notes (21) -- CCSpriteBatchNode

Note: From cocos2d-x Study Notes (21) I used the cocos2d-x version 2.0.2, while the version I used earlier was 1.0. Version 2.0.2 fixes many bugs, including the bugs that occur when CCTMXLayer uses the addChild function ). There are some differences between the two versions, which may cause some inconvenience for everyone to read. I would like to apologize to you here. I hope you can pay more attention to the differences, I will modify the previous article later, how to Use version 2.0.2 on the annotation ...........................

 

When we add sprite objects, they all go through the preparation, rendering, and clearing phases. In the previous content, the method that we use most to add the genie is to call the addChild () function to add. However, when a large number of genie objects are added, using this method will greatly reduce the number of frames per second and affect the smoothness of the game. The following is an example:

[Html]
CCSprite * sprite1 = CCSPrite: create ("sprite1.png ");
AddChild (sprite1 );
CCSprite * sprite2 = CCSPrite: create ("sprite2.png ");
AddChild (sprite2 );
CCSprite * sprite3 = CCSPrite: create ("sprite3.png ");
AddChild (sprite3 );
...
...
CCSprite * sprite100 = CCSPrite: create ("sprite100.png ");
AddChild (sprite100 );
This example is the most commonly used method. The entire process of this method is: {preparation, rendering, clearing}, {preparation, rendering, clearing }......... ....... {preparation, rendering, and cleanup} has been repeating for 100 times. From this, we can find that the preparation and clearing processes only need one time, and the rest is the rendering of 100 times. The following method solves this problem:

[Html]
CCSpriteBatchNode * batch = CCSpriteBatchNode: create ("sprites.png ");
AddChild (batch, 0, kTagBatch );
 
CCArray * array = CCArray: create ();
For (int I = 0; I <100; I ++)
{
CCSprite * sprite = CCSprite: createWithTexture (batch-> getTexture (), CCRectMake (50 * I, 0, 50, 50 ));
Array-> addObject (sprite );
Batch-> addChild (sprite, I); // note that all sprite instances are on the same Z axis.
}
The entire process of using this method is: Prepare, render 100 times, clear, which saves many repeated preparation and clearing processes, greatly improving the efficiency. The image we load is an image set, just like the image below:

You only need to set the CCRectMake parameter in createWithTexture. Note that the size of each role in the figure must be the same.

However, using CCSpriteBatchNode means that all the sprite objects are on the same Z axis of batch, which is a constraint.

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.