Been reading, poking around sample code and docs, but still a bit fuzzy.
Can someone give me the basic 2-3 sentence definition of what this CCSpriteBatchNode is, what it does, and why I should (or shouldn't use it)?
Thanks,
_mike
A fast, easy to use, free, and community supported 2D game engine
Been reading, poking around sample code and docs, but still a bit fuzzy.
Can someone give me the basic 2-3 sentence definition of what this CCSpriteBatchNode is, what it does, and why I should (or shouldn't use it)?
Thanks,
_mike
Taking a shot here, if anyone thinks anything is incorrect just correct me. And I'll go beyond the 2-3 sentences limit.
From CCSpriteBatchNode.h
CCSpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call
Which is why you should use it when applicable.
Instead of each sprite using it's own texture, a lot of different sprites can use the same texture. Since each texture you're using needs to be drawn by OpenGL, this helps speed up performance by reducing the amount of drawcalls.
One example may be when you have a lot of small environment objects. Instead of having each object's texture as a separate iamge file, you can mash them all together into a single one (such as this one http://media.smashingmagazine.com/images/css-sprites/pokemon.gif). You then specify which part of the spritesheet contains the object, f.ex. in the Pokemon spritesheet it might be the fountain, and create a CCSprite with the
-(id) initWithBatchNode:(CCSpriteBatchNode*)batchNode rect:(CGRect)rect
method. The CCSprite then needs to be childed to the BatchNode and the BatchNode to your layer.
The setback of using this is that you can't have two such CCSprites using the same BatchNode on different Z-layers (i.e. one cannot be 'on top' of another (right?)), since they're all drawn at once. So you can't have a tree in the foreground with a rock behind it.
Edit:
Another perk I haven't even gone through is the power-of-two textures stuff. Just trust me that generally mashing a lot of textures into a single one is beneficial. Zwoptex will help you do that: http://zwopple.com/zwoptex/
Wow, although I have read very simliar posts in the past, your's seem to have done the trick. I think I finally *get it*. Thanks a lot. One of my questions was regarding the use of sprite sheets, and if they were mandatory for BatchNodes, and it looks like Yes, that's the whole point. You get performance and memory savings by using the single texture sheets.
Thanks!
_mike
Sprites within the same BatchNode CAN BE on different Z-Layers. The limitation is that all sprites within that BatchNode, can only be in one position, in relation to other Nodes that are not within that specific BatchNode.
Glad I could help nibeck. :)
@MisterX
Oh yeah, when I wrote that thing about the Z-layers it didn't really make sense to me either. With 'one position' you do mean Z-order right? I.e. they can be stacked on top of each other, but not one in the background and one in the foreground? That makes a lot more sense what with the painter's algorithm and whatnot.
MisterX, not quite sure I follow you. I get that individual sprites within a BatchNode can gave different Z-order, but not sure I understand "can only be in one position, in relation to other Nodes that are not within that specific BatchNode"
If I have a series of sprites (let's say different types of chars) in batch layer, and I also have a series of sprites traditionally created and added to the scene with [self add child:]. Are there restrictions as to the position of the sports in the BatchNode and those that are not in the batch node? They can't move freely around each other?
_mike
Actually, they can. I think what he means by 'one position' he means Z-order. I.e. you can have all kinds of different Z-orders on the sprites in the BatchNode, but the BatchNode itself can only have one Z-position. Since the sprites are all children of the BatchNode, you can't have 'one sprite in the foreground and one sprite in the background' in regards to the other sprites on the layer. Is this making sense to you? :)
Ahhh, got it now. Thanks all, excellent info.
_mike
Yes, I was referring to one Z-order position. For example:
[self addChild: backGround z:0];
[self addChild: batchNode z:1];
[self addChild: foreGround z:2];
[batchNode addChild: aSprite z:40];
No matter what z-position you assign to aSprite added to batchNode, it will always be between the backGround and foreGround Sprites (z0 and z2) in regards to the entire layer.
You must log in to post.