CCBatchNode Overview in Cocos2d

I heard that using CCBatchNode is much better for performances in Cocos2d. I have problems understanding how this works. I have several subclasses of CCSprite that represent objects in my game. While I initialize them with the spriteWithFile: method.

I have a sprite containing all my sprites (along with its plist file). How to use it so that I can use the "single call to OpenGL draw"?

If I initialize my sprites, return the frame to CCBatchNode, will it work?

Anyway, I am not good at CCBatchNode, some help would be great!

Hello

+4
source share
1 answer

Here is an example of using CCSpriteFrameCache and CCSpriteBatchNode.

First, configure the general frame cache (think of it as a mapping between the names of your individual images and their rectangular frames in a combined node package):

 [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"spritesheet.plist" textureFile:@"spritesheet.png"]; 

Now create your node batch with a unified image name (this BatchNode makes a single draw for multiple sprites):

 CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"spritesheet.png" capacity:20]; 

Now create a sprite node that passes the name of the individual image. Add it to the node package. (Again, batchnode will make calls for a rally, so this is a performance advantage):

 CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"name_of_your_sprite_in_plist"]; [batchNode addChild:sprite]; 

You can handle the sprite in the same way as a sprite that you created earlier from a single image (but with performance benefits using the node package).

+3
source

Source: https://habr.com/ru/post/1447279/


All Articles