【问题标题】:cocos2d dynamically changing animation speed with CCSpeedcocos2d 使用 CCSpeed 动态改变动画速度
【发布时间】:2012-12-24 10:35:29
【问题描述】:

在一个简单的游戏中,一个角色有一个大约 15 帧的行走动画。基于加速度计的是速度在任一方向的变化。我希望动画相应地加速或减速。我意识到一旦动画被初始化,你就不能改变延迟速度,而是需要创建一个新的动画。但是由于速度几乎是不断变化的,如果我一直重置动画,它总是会卡在同一帧上。

有没有办法找到动画当前所在的帧,停止它,然后创建一个具有相同帧但从上一个动画结束处开始的新动画?

否则有人知道如何实现动态变化的动画延迟时间吗?

编辑:

我在 Learn Cocos2d 2 中的一个示例中尝试使用 CCSpeed 来执行此操作。代码如下:

CCRepeatForever *walkLeftRepeat = [CCRepeatForever actionWithAction:[CCAnimation animationWithSpriteFrames:walkLeftFrames]];

self.walkLeft = [CCSpeed actionWithAction:walkLeftRepeat speed:animationWalkingSpeed];

我收到以下警告:不兼容的指针类型将“CCRepeatForever *”发送到“CCActionInterval *”类型的参数

这是我所遵循的示例:

CCRepeatForever* repeat = [CCRepeatForever actionWithAction:rotateBy];
CCSpeed* speedAction = [CCSpeed actionWithAction:repeat speed:0.5f];

第二次编辑:

然后我尝试了这个:

CCAnimation *walkLeftRepeat = [CCAnimation animationWithSpriteFrames:walkLeftFrames];
CCAnimate *temp = [CCAnimate actionWithAnimation:walkLeftRepeat];
CCAnimate*temp2 = [CCRepeatForever actionWithAction:temp];
self.walkLeft = [CCSpeed actionWithAction:temp2 speed:animationWalkingSpeed];
[_guy runAction:_walkLeft];

最终编辑。通过添加动画延迟来解决它不显示的问题。

这不会给出任何错误,但没有任何反应。 谢谢!

【问题讨论】:

    标签: iphone animation cocos2d-iphone


    【解决方案1】:

    无需重新创建动画。使用带有 CCAnimate 动作的 CCSpeed 动作作为其内部动作。然后你可以修改 CCSpeed 动作的 speed 属性来加快或减慢动画的速度。

    另一种方法是自己推进帧。这很容易做到,您只需要一个预定的更新和一个告诉您何时更改到下一帧的计时器。您只需更改每帧添加到计时器的数量即可加快或减慢动画速度。如果计时器大于特定值,则更改帧并将计时器重置为 0。

    【讨论】:

    • 谢谢!我什至不知道 CCSpeed 的存在。我对如何更改速度属性有点困惑。在类参考的底部,它说明了如何:cocos2d-iphone.org/api-ref/0.9.0-alpha/interface_c_c_speed.html。但我似乎无法弄清楚如何引用 speed 属性。
    • 没关系。我重新阅读了你书中的动画部分并弄清楚了。你可以这样做:'speedAction.speed = 2;'。问题是我没有将我的 speedAction 更改为“CCSpeed*”类型。
    • 我根据书中的例子试过了,还是不行,我更新了问题。谢谢!
    【解决方案2】:

    在 Cocos2d 游戏中可以做到:

    -(void)preLoadAnim
    {
         CCAnimation* animation = nil;
        animation = [[CCAnimationCache sharedAnimationCache]  animationByName:@"ANIMATION_HERO"];
    
        if(!animation)
        {
            CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
    
            NSMutableArray *animFrames = [NSMutableArray array];
    
            for( int i=1;i<=4;i++)
            {
                CCSpriteFrame *frame = [cache spriteFrameByName:[NSString stringWithFormat:@"HeroRun_%d.png",i]];
                [animFrames addObject:frame];
            }
    
            animation = [CCAnimation animationWithSpriteFrames:animFrames];
    
            animation.delayPerUnit = 0.5f; 
            animation.restoreOriginalFrame = NO;
    
            [[CCAnimationCache sharedAnimationCache] addAnimation:animation name:animName];
        }
    }
    
    -(void)runHeroSlowMode
    {
         CCAnimation* animation = nil;
        animation = [[CCAnimationCache sharedAnimationCache]  animationByName:@"ANIMATION_HERO"];
    
        animation.delayPerUnit = 1.0f; 
    
        CCAnimate *animAction  = [CCAnimate actionWithAnimation:animation];
        CCRepeatForever *runningAnim  = [CCRepeatForever actionWithAction:animAction];
        runningAnim.tag = kTagHeroRunAction;
    
        [self stopActionByTag: kTagHeroRunAction];
    
        [heroSprite runAction:runningAnim];
    
    }
    
    
    -(void)runHeroFastMode
    {
         CCAnimation* animation = nil;
        animation = [[CCAnimationCache sharedAnimationCache]  animationByName:@"ANIMATION_HERO"];
    
        animation.delayPerUnit = 0.1f; 
    
        CCAnimate *animAction  = [CCAnimate actionWithAnimation:animation];
        CCRepeatForever *runningAnim  = [CCRepeatForever actionWithAction:animAction];
        runningAnim.tag = kTagHeroRunAction;
    
        [self stopActionByTag: kTagHeroRunAction];
        [heroSprite runAction:runningAnim];
    
    }
    

    【讨论】:

      【解决方案3】:

      我也遇到了同样的问题。在我的游戏中,每当我收集东西时,我的速度都会增加。那时我在当前帧暂停了我的动画,改变了它的延迟时间,然后恢复它....现在它的工作

      【讨论】:

        猜你喜欢
        • 2021-10-03
        • 1970-01-01
        • 1970-01-01
        • 2015-04-30
        • 1970-01-01
        • 1970-01-01
        • 2018-08-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多