0

我正在开发一个游戏,我给用户开火炮的机会。火动画被缓存,但它的宽度为 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;
}
4

3 回答 3

2

CCAnimate actionWithDuration: animation: restoreOriginalFrame:已弃用,请使用此:

创建精灵:

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

创建动画:

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

创建动作:

id action = [CCAnimate actionWithAnimation:animation];

添加到精灵:

永远重复:

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

正常与反向:

[sprite runAction: [CCSequence actions: action, [action reverse], nil]];
于 2013-05-30T15:47:03.813 回答
0

首先,创建你的精灵

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

现在创建动画

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

现在当你想播放动画时,创建一个动画动作

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

并告诉你的精灵运行动画

[sprite runAction:animateAction];
于 2012-04-13T20:32:34.333 回答
0

在 cocos2d 中使用/创建 CCSprites 很容易无意中获得糟糕的性能。考虑到这一点,我建议遵循 cocos2d 动画编程指南:http: //www.cocos2d-iphone.org/wiki/doku.php/prog_guide :animation

于 2012-04-13T20:26:32.670 回答