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;
}