CCSpriteBatchNode for noobz

Forums Programming cocos2d support (graphics engine) CCSpriteBatchNode for noobz

This topic contains 9 replies, has 4 voices, and was last updated by  MisterX 1 year, 9 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
Author Posts
Author Posts
July 13, 2012 at 1:27 pm #242521

nibeck
@nibeck

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

July 13, 2012 at 1:58 pm #381153

SeruK
@seruk

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/

July 13, 2012 at 5:54 pm #381154

nibeck
@nibeck

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

July 13, 2012 at 9:17 pm #381155

MisterX
Participant
@misterx

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.

July 13, 2012 at 9:33 pm #381156

Birkemose
Keymaster
@birkemose

@SeruK pretty much nailed it.

July 13, 2012 at 10:32 pm #381157

SeruK
@seruk

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.

July 14, 2012 at 12:23 pm #381158

nibeck
@nibeck

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

July 14, 2012 at 1:07 pm #381159

SeruK
@seruk

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? :)

July 15, 2012 at 12:22 am #381160

nibeck
@nibeck

Ahhh, got it now. Thanks all, excellent info.

_mike

July 16, 2012 at 4:55 am #381161

MisterX
Participant
@misterx

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.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.