Reputation: 2024
My question is related to how the use of CCSpriteBatchNode works... When you initialize the CCSpriteBatchNode with a file, let's say:
CCSpriteBatchNode *spriteBatch;
spriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"file.pvr.ccz"];
[self addChild:spriteBatch];
And then, here is my doubt... Why do you need to add each sprite that you will use to the CCSpriteBatchNode if these are supposed to be loaded when you call batchNodeWithFile?
Here is the code where you add each sprite:
NSArray *images = [NSArray arrayWithObjects:@"sprite1.jpg", @"sprite2.jpg", @"sprite3.jpg", @"sprite4.jpg", @"sprite5.jpg", @"sprite6.jpg", nil];
for(int i = 0; i < images.count; ++i) {
NSString *image = [images objectAtIndex:i];
float offsetFraction = ((float)(i+1))/(images.count+1);
CGPoint spriteOffset = ccp(winSize.width*offsetFraction, winSize.height/2);
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:image];
sprite.position = spriteOffset;
[spriteBatch addChild:sprite]; //Here is what I mean... Why to do this? Isn't that supposed that they are already loaded in the CCSpriteBatchNode?
}
Thanks!
Upvotes: 0
Views: 2429
Reputation: 1408
That's for optimization. basically when you add a CCSprite to a layer involves an OpenGL call to draw the item (in fact they are 7 i think), so if you have 100 sprites, 100 calls are done. If you add them to a BatchNode it takes only one call to draw all their children's.
Check the docs:
CCSpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call (often known as "batch draw").
A CCSpriteBatchNode can reference one and only one texture (one image file, one texture atlas). Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode. All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call. If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.
Upvotes: 4