Pablo
Pablo

Reputation: 29519

Animating multiple sprites

This is the code for creating single sprite, then animating it:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"crowfl.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"crowfl.png"];
[self addChild:spriteSheet];
NSMutableArray *crowAnimFrames = [NSMutableArray array];
for (int i=1; i<=8; i++) {
    [crowAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"crow%d.png", i]]];
}
CCAnimation *crowAnim = [CCAnimation animationWithSpriteFrames:crowAnimFrames delay:0.1f];
_crow = [CCSprite spriteWithSpriteFrameName:@"crow1.png"];
_crow.position = ccp(windowSize.width + _crow.contentSize.width/2, _crowFlightHeight);
id crowAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:crowAnim]];
[_crow runAction:crowAction];
[spriteSheet addChild:_crow];
...
id crowMoveAction = [CCSequence actions:
                     [CCMoveTo actionWithDuration:5.0 position:ccp(_flipCrow ?   (windowSize.width + _crow.contentSize.width/2) : (-_crow.contentSize.width/2), _crowFlightHeight)],
                     crowMoveComplete,
                     nil];
[_crow runAction:crowMoveAction];

What I need is to create multiple sprites(_crow) and animate them asynchronously. I need to know what part of code can be reusable/shared among multiple sprites and what part should be unique for each animating sprite.

Upvotes: 1

Views: 180

Answers (1)

Guru
Guru

Reputation: 22042

Cache animation in CCAnimationCache and that reduces delay in loading again and again:

CCAnimation* crowAnim = nil;
crowAnim = [[CCAnimationCache sharedAnimationCache]  animationByName:@"crowAnim"];

if(!crowAnim)
{
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"crowfl.plist"];
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"crowfl.png"];
    [self addChild:spriteSheet];
    NSMutableArray *crowAnimFrames = [NSMutableArray array];
    for (int i=1; i<=8; i++) {
        [crowAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"crow%d.png", i]]];
    }

    crowAnim = [CCAnimation animationWithSpriteFrames:crowAnimFrames delay:0.1f];

    [[CCAnimationCache sharedAnimationCache] addAnimation:crowAnim name:@"crowAnim"];

}

_crow = [CCSprite spriteWithSpriteFrameName:@"crow1.png"];
_crow.position = ccp(windowSize.width + _crow.contentSize.width/2, _crowFlightHeight);
id crowAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:crowAnim]];
[_crow runAction:crowAction];
[spriteSheet addChild:_crow];
...
id crowMoveAction = [CCSequence actions:
                     [CCMoveTo actionWithDuration:5.0 position:ccp(_flipCrow ?   (windowSize.width + _crow.contentSize.width/2) : (-_crow.contentSize.width/2), _crowFlightHeight)],
                     crowMoveComplete,
                     nil];
[_crow runAction:crowMoveAction];

Upvotes: 2

Related Questions