Batch Processing genie in Cocos2d-X

Source: Internet
Author: User

Batch Processing genie in Cocos2d-X

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 window 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 window 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 ;}

Execution result:

1. Effect After opening the program: The frame rate is 58.7 after the program is started


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Environment/WwcvF + rSmwO2 + q8Hp1qHCyrz1ydk8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20141009/2014100908181843.png" alt = "\">


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 window 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 ;}

Execution 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

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.