Do program development must take into account the memory optimization, after all, the iphone itself is not very large memory, this section mainly said that the cocos2d development of memory optimization, specific performance in, both the same sprite (image) can only render once, or can not be rendered only once.
The following is the procedure of the program, first block the previous code.
let's start with a brief introductionCcspritebatchnode optimize game rendering efficiency, in the >ccspritebatchnode ccsprite will only be rendered 1 times, so you can improve the game fps. Restrictions: Add to ccspritebatchnode font-family:arial line-height:20px in ccsprite You must use the same texture map.
Specific code
ccspritebatchnode *node =ccspritebatchnode::Create(" Peashooter1.tiff "); // render the file first
This,addChild(node);
ccsprite *plant = ccsprite::create ("Peashooter1.tiff"); // Creating Sprites
Plant->setposition (CCP(a));
Node->addchild (plant); // Add to node in
Run:
You can see that adding a sprite at this time the fps is 1. Add another file below the same wizard:
ccspritebatchnode *node =ccspritebatchnode::Create(" Peashooter1.tiff "); // render the file first
This,addChild(node);
ccsprite *plant = ccsprite::Create("Peashooter1.tiff"); // Creating Sprites
Plant->setposition(CCP(a));
Node->addChild(plant); // Add to node in
// Create a sprite again
ccsprite *plant1 = ccsprite::Create("Peashooter1.tiff"); // Creating Sprites
Plant1->setposition(CCP(+));
Node->addChild(plant1);
return true;
Run:
You can see that two sprites have been created, but the fps is still 1, which is the role of Ccspritebatchnode, which guarantees that all the same sprites added to the class will be rendered only once.
Since the same sprite can only render, then the different files of the wizard can only be rendered once, the answer is yes, see the following approach.
This time to use the content of the article (v), the specific method is that we can be a lot of different pictures to synthesize a large map, and then add this large map to the Ccspritebatchnode, then the rendering of the large map of the small map will only be rendered once, to see the specific program practice:
///* If you want to make different images render only once, use the frame caching mechanism to render several different pictures * /
ccspriteframecache::sharedspriteframecache(),addspriteframeswithfile ("person.plist"); // cache Large Images first
ccspritebatchnode *node1 =ccspritebatchnode::Create("person.png "); // render to a large picture
This,addChild(Node1);
ccspriteframe *frame =ccspriteframecache::sharedspriteframecache ()spriteframebyname(" png");
ccsprite *plant3 =ccsprite::createwithspriteframe(frame);
Plant3->setposition(CCP);
Node1->addChild(PLANT3);
ccspriteframe *frame1 =ccspriteframecache::sharedspriteframecache ()spriteframebyname(" login . png");
ccsprite *plant4 =ccsprite::createwithspriteframe(frame1);
Plant4->setposition(CCP);
Node1->addChild(PLANT4);
The name of all the small pictures of this large figure:
Run:
We can see that we have added two more pictures, and the two images are not the same, but fps still adds 1 to 2, which means that the newly added two sprites have only been rendered once, and now we find that the frame caching mechanism is very powerful and is often used in game development.