Cocos2d-x ios game development (6) Optimization of rendering, cocos2d-xios

Source: Internet
Author: User

Cocos2d-x ios game development (6) Optimization of rendering, cocos2d-xios

The memory optimization must be taken into account for program development. After all, the memory of the iphone itself is not very large. This section mainly describes the memory optimization of cocos2d development, you can render the same sprite (image) only once or only once.

The following is the procedure of the program, first blocking the previous code.

The CCSpriteBatchNode class is used to optimize game rendering efficiency. All CCSprite in CCSpriteBatchNode will only be rendered once, thus increasing the FPS of the game. Restriction: CCSprite added to CCSpriteBatchNode must use the same texture image.

Code

CCSpriteBatchNode * node = CCSpriteBatchNode: create ("Peashooter1.tiff"); // first render the file

This-> addChild (node );

CCSprite * plant = CCSprite: create ("Peashooter1.tiff"); // create an genie

Plant-> setPosition (ccp (300,300 ));

Node-> addChild (plant); // Add to node

Run:


You can see that the FPS of adding an genie is 1 at this time. Next, add a file with the same genie:

CCSpriteBatchNode * node = CCSpriteBatchNode: create ("Peashooter1.tiff"); // first render the file

This-> addChild (node );

CCSprite * plant = CCSprite: create ("Peashooter1.tiff"); // create an genie

Plant-> setPosition (ccp (300,300 ));

Node-> addChild (plant); // Add to node

// Create another genie

CCSprite * plant1 = CCSprite: create ("Peashooter1.tiff"); // create an genie

Plant1-> setPosition (ccp (400,400 ));

Node-> addChild (plant1 );

Return true;

Run:


We can see that the two genie have been created, but the FPS is still 1, which is the role of CCSpriteBatchNode. It ensures that the genie added to the class will only be rendered once.


Since the same genie can only be rendered, different file genie can only be rendered once. The answer is yes. See the following practice.

In this case, we will use the content mentioned in Article (5). The specific practice is that we can first combine many different images into a large image, and then add this big image to CCSpriteBatchNode, in this case, the small images in the large graph will be rendered only once. See the specific procedure:

/// * If You Want To render different images only once, you must use the frame cache mechanism to render multiple different images */

CCSpriteFrameCache: sharedSpriteFrameCache ()-> addSpriteFramesWithFile ("Person. plist"); // cache the large image first

CCSpriteBatchNode * node1 = CCSpriteBatchNode: create ("Person.png"); // render to a large image

This-> addChild (node1 );

CCSpriteFrame * frame = CCSpriteFrameCache: sharedSpriteFrameCache ()-> spriteFrameByName (" .png ");

CCSprite * plant3 = CCSprite: createWithSpriteFrame (frame );

Plant3-> setPosition (ccp (200,200 ));

Node1-> addChild (plant3 );

CCSpriteFrame * frame1 = CCSpriteFrameCache: sharedSpriteFrameCache ()-> spriteFrameByName (" .png ");

CCSprite * plant4 = CCSprite: createWithSpriteFrame (frame1 );

Plant4-> setPosition (ccp (500,500 ));

Node1-> addChild (plant4 );

The names of all small images in this big chart:


Run:


We can see that two more images are added, and these two images are not the same, but FPS still adds 1 to 2, it indicates that the newly added two genie are rendered only once. Now we find that the frame cache mechanism is very powerful and is often used in game development.



A few simple questions about the game engine of Cocos2D-X,

1. Theoretically, the cocos2d-x official indicated that the supported platform without a doubt you can transplant, this is its cross-platform advantage.
2. The cross-platform feature of cocos2d-x is embodied in the use of C ++ as programming language development, and provides a solution for compiling and running of various platforms. You can develop your code in any development environment. Your writing process is nothing more than using C ++ to write code logic. Of course, you finally want to deploy the service on various platforms and perform some special operations. For example, for ios, You need to configure the development environment in mac, install xcode, install certificates, etc. For android, you need to install NDK and ADT. I will not go into details. In the specific process, we searched for tutorials on the Internet step by step. First, get caught in a development environment and target platform you are familiar with, such as using VS for development, compiling to Android or xcode for development, and running to ios.

New starter, cocos2d-x & ios

There is no difference between using xcode directly and installing cocos2d-x with xcode.

Ios development with cocos2d-x, you do not need to learn object-c.

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.