简体   繁体   English

cocos2d CCSprite +动画重复?

[英]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. 火焰动画被缓存,但它的宽度为25px,火焰有时会达到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? 所以,如果我发射4个大炮,宽度为150px(类似6级),我目前正在创建4x6 = 24个CCSprites,并运行24个动画,每个10帧(缓存帧)...问题是它变得非常慢,火力动画不会同时开始/结束,游戏中的其他动画变慢......你明白了......有什么想法可以改善这个吗? 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]];) (对不起,我忘了这个的解释[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;
}

CCAnimate actionWithDuration: animation: restoreOriginalFrame: is deprecated, use this : CCAnimate actionWithDuration: animation: restoreOriginalFrame:已弃用,请使用:

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

It's pretty easy to unintentionally get bad performance using/creating CCSprites in cocos2d. 在cocos2d中使用/创建CCSprites无意中会导致性能下降,这很容易。 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 考虑到这一点,我建议遵循动画的cocos2d编程指南: http//www.cocos2d-iphone.org/wiki/doku.php/prog_guide :animation

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM