Game Development Reference
In-Depth Information
Notice that in Listing 6-1 the CCSpriteBatchNode takes a file as an argument,
even though the CCSpriteBatchNode itself isn't displayed. It's more like a
CCLayer in that regard, except that you can add only CCSprite nodes to it. The
reason it takes an image file as an argument is that all CCSprite nodes added to the
CCSpriteBatchNode must use the same texture. If you accidentally add a sprite
with a different texture, or a non-sprite node to the batch node, you'll see one of these
two error messages in the Debug Console:
'NSInternalInconsistencyException', reason: 'CCSprite is not using the same texture id'
'NSInternalInconsistencyException', reason: 'CCSpriteBatchNode only supports CCSprites as children'
When to Use CCSpriteBatchNode
You can use CCSpriteBatchNode whenever you display two or more CCSprite s
of the same kind. The more CCSprite s you can group together, the greater the bene-
fit of using CCSpriteBatchNode .
There are limitations, though. Because all the CCSprite nodes are added to the CCS-
priteBatchNode , all CCSprite nodes added to it will be drawn at the same z-or-
der (depth). If your game is supposed to have bullets flying behind and in front of en-
emies, you would have to use two CCSpriteBatchNode s to group the bullet sprites
of the lower and the higher z-order independently.
Another drawback is that all CCSprite s added to the CCSpriteBatchNode need
to use the same texture. But that also means the CCSpriteBatchNode becomes
most important when you're using a texture atlas. With a texture atlas, you're not lim-
ited to drawing only one image; instead, you can add a number of different images to
the same texture atlas and draw all of them using the same CCS-
priteBatchNode —speeding up the rendering of all the images of the same texture
If all your game's images could fit into the same texture atlas, you could compose al-
most your entire game using just a single CCSpriteBatchNode (although this
would be the rare exception).
Search Nedrilad ::

Custom Search