Agustin
Agustin

Reputation: 173

cocos2d CCSprite + animation repeat?

I'm developing a game and I give the user the oportunity to fire cannons. the fire animation is cached, but it has 25px width and the fire sometimes gets to 150px.

so, if I fire 4 cannons, with 150px width (sth like level 6) I'm currently creating on the fly 4x6 = 24 CCSprites, and running 24 animations of 10 frames each(cached frames)... the problem is that it gets really slow, the fire animations dont start/finish at the same time, the other animations in the game get slower... you get the idea... any ideas to improve this? thank you all!

this is the code:

NSMutableArray *sprites = [[NSMutableArray alloc] init];
NSMutableArray *anims = [[NSMutableArray alloc] init];

int spr = 0;
for( int i=0; i< [cannons count]; i++ ) {
   CCSprite *cannon = [cannons objectAtIndex:i];
   for( int j=1; j<=cannonRange; j++ ) {

        [sprites addObject:[[CCSprite alloc]init]];
        NSString *anim = @"cannon/fire";
        if( j == cannonRange )
            anim = @"cannon/fire_end";
        [anims addObject:[[Animation findAnimation:anim] getAnimation]];

        float x=cannon.position.x, y=cannon.position.y+25*j;

        ((CCSprite *)[sprites objectAtIndex:spr]).position = ccp(x,y);
        [layer addChild:((CCSprite *)[sprites objectAtIndex:spr]) z:50];

        spr++;
    }
}
for( int k=0; k<[sprites count]; k++){
    [[sprites objectAtIndex:k] runAction:[anims objectAtIndex:k]];
}

(sorry, I forgot the explanation of this [anims addObject:[[Animation findAnimation:anim] getAnimation]];)

-(CCAnimate *) getAnimation {

    NSMutableArray *animFrames = [[NSMutableArray alloc]init];
    for(int i = 1; i < size; ++i)
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[self fileName:i]]];

    CCAnimation *animation = [[CCAnimation alloc] initWithFrames:animFrames delay:[self getDelay]];

    CCAnimate *animate = [[CCAnimate alloc] initWithAnimation:animation restoreOriginalFrame:NO];

    return animate;
}

Upvotes: 0

Views: 2576

Answers (3)

VivienCormier
VivienCormier

Reputation: 1133

CCAnimate actionWithDuration: animation: restoreOriginalFrame: is deprecated, use this :

Create sprite :

CCSprite *sprite = [CCSprite spriteWithFile:@"img.png"];
[self addChild:sprite];

Create animation :

CCAnimation* animation = [CCAnimation animation];   
[animation addSpriteFrameWithFilename:@"frame1.png"];
[animation addSpriteFrameWithFilename:@"frame2.png"];
[animation addSpriteFrameWithFilename:@"frame3.png"];
animation.delayPerUnit = 0.3f;
animation.restoreOriginalFrame = YES;

Create action :

id action = [CCAnimate actionWithAnimation:animation];

Add to sprite :

ex repeat forever :

[sprite runAction:[CCRepeatForever actionWithAction:action]];

ex normal with reverse :

[sprite runAction: [CCSequence actions: action, [action reverse], nil]];

Upvotes: 2

Nick Bull
Nick Bull

Reputation: 4286

First, create your sprite

CCSprite *sprite = [[CCSprite alloc] init];

Now create the animation

anim = [[CCAnimation animation] retain];
[anim addFrameWithFilename:@"frame1.png"];
[anim addFrameWithFilename:@"frame2.png"];
[anim addFrameWithFilename:@"frame3.png"];
...

Now when you want to play the animation, create an animation action

id animateAction = [CCAnimate actionWithDuration:2.0f animation:anim restoreOriginalFrame:NO];

And tell your sprite to run the animation

[sprite runAction:animateAction];

Upvotes: 0

MechEthan
MechEthan

Reputation: 5703

It's pretty easy to unintentionally get bad performance using/creating CCSprites in cocos2d. With that in mind, I would recommend following the cocos2d programming guide on animation: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:animation

Upvotes: 0

Related Questions