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!
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: