Batch Processing genie using common methods
Add the following code to Sprite. h:
# Ifndef _ sprite_scene_h __# DEFINE _ sprite_scene_h __# include "cocos2d. H "using_ns_cc; Class sprite: Public cclayer {public: // initialization layer bool Init (); // create a scenario static ccscene * scene (); // respond to the touch bool sprite :: cctouchbegan (cctouch * touch, ccevent *); // used to create: create_func (sprite);}; # endif/_ sprite_scene_h __
Add the following code to Sprite. cpp:
# Include "Sprite. H "using_ns_cc; // create scenario ccscene * sprite: Scene () {// create scenario ccscene * scene = ccscene: Create (); // create a layer sprite * layer = sprite: Create (); // Add the layer to the scene-> addchild (layer); // return scene ;} // initialize the instance handle bool sprite: Init () {// initialize the parent cclayer: Init (); // obtain the form size ccsize winsize = ccdirector :: shareddire()-> getwinsize (); // sets the coordinate ccpoint ptcenter = CCP (winsize. width/2, winsize. height/2); // create a batch processing node container ccspritebatchnode * batch = ccspritebatchnode: Create ("closenormal.png"); // Add the genie to the layer addchild (batch ); // use textures to create the sprite * sprite = ccsprite: createwithtexture (batch-> gettexture (); // Add the sprite to the container batch-> addchild (sprite ); // set the sprite position sprite-> setposition (ptcenter); // set the touch settouchenabled (true); settouchmode (kcctouchesonebyone); Return true;} // response to the touch bool sprite :: cctouchbegan (cctouch * touch, ccevent *) {// obtain the form size ccsize winsize = ccdirector: shareddirector ()-> getwinsize (); // 100 cycles, create 1000 genie for (INT I = 0; I <1000; I ++) {// create genie ccsprite * sprite = ccsprite: Create ("closenormal.png "); // Add the genie to the layer addchild (sprite); // set the sprite position sprite-> setposition (CCP (ccrandom_0_1 () * winsize. width, ccrandom_0_1 () * winsize. height);} return true ;}
Running result:
1. Effect After opening the program: The frame rate is 58.7 after the program is started
2. After three mouse clicks, the batch processing sprite frame rate is reduced.
Batch Processing genie using textures
Add the following code to Sprite. h:
# Ifndef _ sprite_scene_h __# DEFINE _ sprite_scene_h __# include "cocos2d. H "using_ns_cc; Class sprite: Public cclayer {public: // initialization layer bool Init (); // create a scenario static ccscene * scene (); // respond to the touch bool sprite :: cctouchbegan (cctouch * touch, ccevent *); ccspritebatchnode * _ batch; // used for creating: scenes, menus, layers, and other things create_func (sprite );}; # endif/_ sprite_scene_h __
Code in Sprite. cpp
# Include "Sprite. H "using_ns_cc; // create scenario ccscene * sprite: Scene () {// create scenario ccscene * scene = ccscene: Create (); // create a layer sprite * layer = sprite: Create (); // Add the layer to the scene-> addchild (layer); // return scene ;} // initialize the instance handle bool sprite: Init () {// initialize the parent cclayer: Init (); // obtain the form size ccsize winsize = ccdirector :: shareddire()-> getwinsize (); // sets the coordinate ccpoint ptcenter = CCP (winsize. width/2, winsize. height/2); // batch processing genie ccspritebatchnode * batch = ccspritebatchnode: Create ("closenormal.png"); addchild (batch); _ batch = Batch; ccsprite * sprite = ccsprite:: createwithtexture (batch-> gettexture (); batch-> addchild (sprite); sprite-> setposition (ptcenter); settouchenabled (true); settouchmode (kcctouchesonebyone ); return true;} // response to the touch bool sprite: cctouchbegan (cctouch * touch, ccevent *) {ccsize winsize = ccdirector: shareddirector ()-> getwinsize (); for (INT I = 0; I <1000; I ++) {ccsprite * sprite = ccsprite: createwithtexture (_ batch-> gettexture ()); _ batch-> addchild (sprite); sprite-> setposition (CCP (ccrandom_0_1 () * winsize. width, ccrandom_0_1 () * winsize. height);} return true ;}
Running result:
1. Effect After opening the program: The frame rate is 58.7 after the program is started
2. After multiple clicks, the frame rate does not change significantly
Note: ccspritebatchnode is also a container, but it can only accommodate ccsprite objects and requires these genie to come from the same texture.
Zookeeper
Batch Processing genie in Cocos2d-X