[IOS-Cocos2d game development 9] Explain ccspritebatchnode and TP tool ". PVR. CCZ", ". plist" shared ultimate genie optimization and attention

Source: Internet
Author: User

Li huaming himiOriginal, reprinted must be explicitly noted:

[Heimi gamedev block]Link: http://www.himigame.com/iphone-cocos2d/465.html

In the previous sections, due to time constraints, I just shared some of the problems I encountered, so today I will take the time to write a frequently used Genie and some frequently-used and paid attention knowledge; since the cocos2d tutorial is quite complete, today we will share some notes and details about himi;

First of all, I am very familiar with ccspritebatchnode for kids shoes that have used genie. At least everyone will know that it can optimize the Genie. But as for the optimization principle, let's briefly describe:

Generally, [cclayer * addchild: ccsprite *] is used directly when the genie ccsprite is used. If we create one hundred genie, the current cclayer will be drawn separately for 100 genie;

If you use the Genie set-ccspritebatchnode, you can directly add the set ccspritebatchnode to the cclayer. Even if you create one hundred genie, we will add 100 genie to the set. [ccspritebatchnode * addchild: ccsprite;

Differences between the two:

1. directly use cclayer to add genie. There are several genie on cclayer, so the underlying layer will draw several genie;

The underlying rendering method is as follows:

For (INT I = 0; I <100; I ++) {open-draw-close ;}

However, when using a set, the cclayer only needs to render the set once,

The underlying rendering method is as follows:

Open-draw (drawing 100 times)-close

From the two methods above, we can see the difference between the two. The second method saves 99 Open and Close processes using the Genie set to achieve optimization;

The last time I attended the fourth IOS Developer Conference, cocos2d-x's Zhang Zhe highlighted this point in his speech, he gave the following data on the iPad in two ways:

Draw 3000 genie on the screen and use the cclayer method to add the genie directly. The number of frames drops from 60 to less than 20;

Draw 3000 genie on the screen and use the ccspritebatchnode rendering method. The number of frames remains at about 50;

Of course, himi has also been tested in person, but since the real machine debugging has not been applied, it can only be tested on the simulator. It is found that 300 genie are simultaneously drawn, and the number of frames in both modes is consistent, there is no difference, not because there is no difference, but because in the IOS simulator, the number of frames is related to the number of genie in the screen, and the real machine will obviously reflect the difference between the two;

2. Although ccspritebathnode can be used for optimization, pay attention to the following points:

When initializing the ccspritebatchnode set, an image resource (or PVR file) will be loaded. Therefore, the child genie that restricts the set must use the image loaded by the set, otherwise, a warning is given;

3. Pay attention to the usage of ccspritebatchnode. Because the genie are stored in the collection, the node (genie) in the ccspritebatchnode of this set will be on the same Z axis and in the same depth;

I have introduced the role of the texturepacker tool in the previous chapter. Here I will not describe how to use it, but I will talk about the two main points, generally, the texturepacker tool packs a lot of Sprite images or action frames together into ". PVR. CCZ ",". plist ","-HD. PVR. CCZ "and"-HD. plist files, two of which are used in the HD version (940*480) generated by the tool;

As ". PVR. the CCZ format is the most compressed and most suitable for iOS resource file formats ". the usage of plist files is also very simple. The Code is as follows:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"XX.plist"];

The above code sets ". PVR. CCZ "two images are packaged into". PVR. CCZ ",". after the two plist files, put the two files under the project (the two images do not need to be), and then use the code just now to load the resource package into the cache, you just need to load the plist file, PVR. CCZ does not need to be loaded, but it also needs to be placed under the project. Then you can create the genie in the following form when creating the genie:

 CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"himi.png"];

From the code above, we can see that this method of creating the genie is created using the frame cache. That is to say, once many of the same genie are loaded, this method is used to optimize the running memory, the easiest thing to think of is creating a bullet;

If you use this resource file in combination with ccspritebathnode, you must create the following collection during loading:

CCSpriteBatchNode spritesBgNode = [CCSpriteBatchNode batchNodeWithFile:@"xx.pvr.ccz"];[CCLayer* addChild:spritesBgNode];    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"xx.plist"];

At last, I would like to emphasize three important points:

1. although the texturepacker tool can generate @ 2X and-HD files, the tool will generate all the sprite images you import as-HD resource packages, the corresponding non-HD is only the resource with half the current image size;

2. if your project needs to load a "himi.png" graph, then in the cocos2d engine development project, first go to the resource to find the "himi-hd.png" image, if you can't find it, you will find the "himi@2x.png" graph, so here you must pay attention to the Resource Name and use;

3. Do not add the following code when using the PVR. CCZ and plist files in cocos2d 1.0.0:

[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default];

4. When you use the texturepacker tool to select and package it into the PVR. CCZ format, you will be prompted to add the following code:

[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];

The original tool prompt is as follows:

[Cctexture2d pvrimageshavepremultipliedalpha: Yes;

Do not forget to check the penultimate option of TP before packaging TP in PVR format ~;

Okay, this is the end of the article. Continue to be busy ......

Related Article

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.