Search code examples
objective-cipadcocos2d-iphoneccsprite

How do I properly use CCSpriteFrameCache and CCSpriteBatchNode?


I do not understand what I do exactly when I add a CCSpriteFrameCache or CCSpriteBatchNode to my cocos2d application. Can somebody please explain the following points (it would be helpful if you could explain a few; please write the corresponding letter in front of your answer according to which question you are answering):

[all questions imply the achievement of best performance and lowest memory-use]

a) Is it crucial to create spritesheets for every single layer ? (For example: Menu - own spritesheet, GameLayer - own spritesheet...)

b) Can somebody explain why I have to add sprites to the batch node, and what a batch node generally is ?

b1)So, why can't I just do something like:

      [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"menusprites.plist"];
       CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"menusprites.png"];
      [self addChild:spriteSheet];

And then just add sprites to my layer by calling

CCSprite *mySprite = [CCSprite  spriteWithSpriteFrameName:@""]; 
[self addChild:mySprite];

without adding them to the batch node ? Because from what I understand it works like this :

I add my spritesheet with all the sprites on it to the screen. My app then goes into the plist and looks for the coordinates of the sprite I want to display and then places it on the screen. So why should I call

[spriteSheet addChild:mySprite];

?

c) How do I then get rid of the spritesheet for memory purposes when I do not need it anymore ?


Solution

  • a) It is best to create as few spritesheets (CCSpriteBatchNodes) as is possible. Sprite batching reduces draw calls. Draw calls are expensive. Still, every batch node creates one draw call. So you want to use as few as possible because the ultimate goal is to keep draw calls as low as possible.

    b) The CCSpriteBatchNode renders all of its children in one go, in one batched draw call. That's why you need to add sprites to the batch node so it can render them all together. Only sprites using the same texture as the batch node can be added to a batch node, because you can only batch draw from the same texture. Whenever the engine has to switch from one texture to another, it issues a new draw call.

    b1) You can't do this because the batch node renders its children. If you add the sprites to any other node, each sprite draws itself, which means one additional draw call per sprite. And the sprite batch node has nothing to do.

    c) The CCSpriteBatchNode is just a regular node. You can remove it from the scene like any other node. The texture and sprite frames are cached in the CCTextureCache and CCSpriteFrameCache singleton classes. If you want to remove the textures and sprite frames from memory, you have to do it through the cache classes.