我正在开发一个游戏,我给用户开火炮的机会。火动画被缓存,但它的宽度为 25 像素,有时火会达到 150 像素。
所以,如果我发射 4 门大炮,宽度为 150 像素(类似于第 6 级),我目前正在动态创建 4x6 = 24 个 CCSprite,并运行 24 个每个 10 帧的动画(缓存帧)......问题是它变得非常慢,火焰动画不会同时开始/结束,游戏中的其他动画变得更慢......你明白了......有什么想法可以改进吗?谢谢你们!
这是代码:
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]];
}
(抱歉,我忘了这个[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;
}