Sprite Sheet vs CCSprite vs CCSpriteBatchNode vs CCTexture2D -> When to use which?

I am confused when to use a sprite sheet and CCSpriteBatchNode and CCTexture2D.

I have a lot of sprites in my game. Some of them have 3 states that the sprite can be in.

So it seems that one needs to do the following:

  1. create a sprite sheet. load the sprite sheet, create sprites from the sprite sheet as needed.

  2. If a sprite has 3 states then create a CCTexture2D from texture cache and set the sprite texture from the CCTexture2D. This lets me change the texture on the fly and update the sprite?

When does one use CCSpriteBatchNode? When I have to create many of the same Sprite?

Thanks for helping with the confusion!

I’d like to have a stronger understanding of this as well.

In one prototype I just set sprite frames on the CCSprite from the CCSpriteFrameCache and add the CCSprite directly to the layer, another time I made the CCSprite a child of the CCSpriteBatchNode.

Is the purpose of CCSpriteBackNode is if you have a ton of CCSprites using the same sprite? If so, why wouldn’t that work with the CCSpriteFrameCache?

Thanks,
C.J.

Is the purpose of CCSpriteBackNode is if you have a ton of CCSprites using the same sprite? If so, why wouldn’t that work with the CCSpriteFrameCache?
Actually it does work. I use CCSpriteFrameCache to preload the majority of graphics assets, then I create sprites using CCSprite::createWithSpriteFrame and add them to the batches.

Sorry, my statement was a little misleading. I understand that it works, but I’m trying to understand the benefit of using CCSpriteBatchNode instead of just using CCSpriteFrameCache to assign sprites to a lot of sprites that are added to a layer. Basically, when is it appropriate to use a CCSpriteBatchNode?

As far as I understand, it’s incorrect to compare CCSpriteFrameCache and CCSpriteBatchNode.
CCSpriteFrameCache, as the name suggests, is the cache for all sprite frames. Sprite frame is a texture plus a corresponding rect. For instance you’ve got several textures merged in a single atlas, thus sprite frames will have the same texture and different rects. You can pull a sprite frame from the cache and apply it to a desired sprite.
CCSpriteBatchNode allows drawing several sprites in a single draw call. It’s important to minimize the number of draw calls, since the draw call itself and accompanying setups (texture binding, shader binding etc) are quite expensive.

Using CCSpriteBatchNode may be inconvenient sometimes.
First of all, the sprites in the batch should share the same texture. It’s not a problem if you have a texture atlas. But it may become a problem if the texture in the atlas should be a tiling texture (it can be solved by a custom shader, but still it’s PITA).
Next problem is that you can’t assign any parent except the batch node or a sprite in the same batch. Thus if the parent and the child do not belong to the same batch node, you’ll have to update child’s transformation according to the parent’s one manually (or I missed something).
Another problem is that since the batch node is drawn in a single draw call, it’s impossible to draw something in between the sprite of that batch. E.g. you’ve got some cloud sprites and a sun sprite in one batch node, also you have the sprite of mountains and you want to draw the sun first, then the mountains and then the clouds. Having clouds and the sun in batch node won’t allow doing this.
In other cases using CCSpriteBatchNode is highly recommended. Use CCSpriteBatchNode every time you can and if it doesn’t not break things and make your life too hard :slight_smile:

Good information, there’s still a bit I need to learn about this stuff :).