I have a CCSpriteBatchNode
with a configuration like this:
CCSpriteBatchNode
ChildA1
ChildB1
ChildB2
....
ChildB999
ChildA2
ChildA3
...
Where all children (ChildA1,ChildB1,ChildA2...) are CCSprite
objects. The CCSpriteBatchNode
and all children but ChildA1
is created via:
[CCSprite spriteWithSpriteFrameName:@"FileName.png"];
ChildA1
is created like this:
// Create Parent Sprite
CCSprite* childA1 = [[CCSprite alloc] init];
childA1.contentSize = CGSizeMake(37.5,37.5);
childA1.anchorPoint = ccp(0,0);
[batchNode addChild:childA1 z:0 tag:1];
// Add Child Sprites
[childA1 addChild:childB1 z:0 tag:1];
[childA1 addChild:childB2 z:0 tag:1];
[childA1 addChild:childB3 z:0 tag:1];
// Continue adding childB4-childB999
Note: This renders just fine, and I see the output I expect, where childB1
's position is relative to childA1
, and moving childA1
results in childB1
moving.
My question is this: Will I see a performance gain in the drawing each of childB1
-childB999
? From what I understand, the CCSpriteBatchNode
optimizes the drawing of all children within a CCSpriteBatchNode
by drawing all its child CCSprites
together. Does this also apply to the children of those CCSprites
?
For those who want to know why I'm doing this:
There are many layers within this game and grouping CCSprites
inside of a CCSprite
within a CCSpriteBatchNode
allows me to manipulate a group of CCSprites
by manipulating only the parent of that group of sprites.
Short answer is, yes, CCSpriteBatchNode
will make one draw call for all child nodes including all indirect descendants.
However, whether or not this performs better than using regular CCSprites
depends on how often you are modifying the sprites. When you use a CCSpriteBatchNode
, every time you modify a sprite, it will need to recalculate the texture atlas quad coordinates for that sprite and all its children using the CPU rather than the GPU. For your example, if you move the position of ChildA1
, the coordinates for ChildB1
to ChildB999
will be recalculated before the next frame is rendered. In most applications, the reduction in openGL draw calls out weights the cost of the extra calculations, as draw calls are relatively expensive, but ultimately it will depend on your application and how it uses the sprites -- so I would suggest taking actual measurements for your application if this is a performance bottleneck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With