I am wondering how to optimize the usage of CCSpriteBatchNode. In other words I understand that:
- 1) Each CCSpriteBatchNode instance performs one call to the draw method, resulting in a reduction of OpenGL calls and hence significant performance improvement
- 2) Each CCSpriteBatchNode can refer to one and only texture atlas
What I am not 100% sure and I would like your answer is:
3) If I have one texture atlas, e.g. game-art-hd.png, and create several CCSpriteBatchNode in various classes will I get mutliple draw calls? In other words, I assume that each instance of CCSpriteBatchNode will call its own draw method, resulting in multiple GL drawing calls and less performance than having one shared batch node. Am I right?
- 4) If I am using an animation made of multiple frames on a Sprite, I guess I should add the animation frames to the Sprite batch node. How can I do so?
Following there is a code snippet on how I normally animate a sprite. As can be noticed the sprite frames are not added to the sprite batch node. For better performance I should probably do this at initialization time. Is this correct?
NSMutableArray* frames = [[NSMutableArray alloc]initWithCapacity:2]; for (int i = 0; i < 4; i++) { NSString*bulletFrame = [NSString stringWithFormat:@"animation-%i.png", i]; CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:bulletFrame]; [frames addObject:frame]; } CCAnimation* anim = [CCAnimation animationWithFrames:frames delay:0.1f]; CCAnimate* animate = [CCAnimate actionWithAnimation:anim]; CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate]; [self runAction:repeat]; }
5) Partially refering to 4. do you confirm that is prefer to avoid adding and removing sprites to a sprite batch node at runtime?
6) Will CCSpriteBatchNode consider only sprites that have visible set to true or sprites that have a position that is actually outside the screen area?
Other considerations on 3
In order to address my assumption in 3., and reduce the number of CCSpriteBatchNode instances, my solution would follow what suggested by @Suboptimus in this answer. I like the suggested approach of initializing classes that want to share the same batch node with the MainScene class, rather than having them to access the MainScene via self.parent.parent.(...).parent.finallysharedbatchNode
Other people would instead suggest to refer to the MainScene via using self.parent.....parent and casting it correctly.
Is this the best approach also in Software Engineering terms?
I prefer making it clear where the sprites are added by using an explicit reference to the MainScene class.. this should help if I am working in team or if I change the class hierarchy. But the downside of this is that I "need" to store a reference to it if I want to add subsequently sprites to the batch node, resulting in more code to maintain.
The reason I am asking this question is that if find a slight clash between my traditional "Software Engineering" mind and the "Cocos2d parent-node" hierarchy approach. I am new to game programming and I'd like to understand which approach is the one that experienced game developers that work in large teams use :). H