Aram Kocharyan
Aram Kocharyan

Reputation: 20431

Is batching animation sprites more efficient with a single sprite? (cocos2d)

In the cocos2d programming guide there is the following code:

CGSize s = [[CCDirector sharedDirector] winSize];
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"grossini_dance_01.png"];
sprite.position = ccp( s.width/2-80, s.height/2); 

CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"animations/grossini.png"];
[batchNode addChild:sprite];
[self addChild:batchNode];

NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 15; i++) {
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"grossini_dance_%02d.png",i]];
    [animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:@"dance" delay:0.2f frames:animFrames];
[sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO] ]];

It adds a simple animation in the form of an array of frames and adds the sprite which will animate these frames into a CCSpriteBatchNode. My question is: will batch drawing a single animated sprite be any more efficient than not using batching at all? Since there is only one frame drawn at each draw and only one object, I would think not. The only benefit I think would be if you added more than one object - so that they could be drawn at their frame coordinates from the same texture in a single draw. Is my reasoning correct?

Upvotes: 1

Views: 473

Answers (1)

Aram Kocharyan
Aram Kocharyan

Reputation: 20431

Thanks to replies here:

http://www.cocos2d-iphone.org/forum/topic/29354?replies=3#post-144515

At least one person has confirmed it has no benefit whatsoever with one object, but may reduce performance slightly from the added complexity.

Upvotes: 1

Related Questions