cocos2d 使用 CCSpeed 动态改变动画速度

cocos2d dynamically changing animation speed with CCSpeed(cocos2d 使用 CCSpeed 动态改变动画速度)

本文介绍了cocos2d 使用 CCSpeed 动态改变动画速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a simple game, a character has a walking animation of around 15 frames. Based on the accelerometer is speed in either direction changes. I wanted the animation to speed up or slow down accordingly. I realize that once the animation is initialized, you can't change the delay speed, and instead you need to create a new animation. But since the speed changes almost constantly, if I kept reseting the animation, it would always be stuck on the same frame.

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

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

Otherwise does anybody have an idea of how you could go about implementing a dynamically changing animation delay time?

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

I have tried using CCSpeed to do this from an example in Learn Cocos2d 2. Here is the code:

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

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

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

I get the following warning: Incompatible pointer types sending 'CCRepeatForever *' to parameter of type '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];

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

Final edit. This problem of it not showing up was fixed by adding a delay to ccanimation.

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

This doesn't give any error's but nothing happens. Thanks!

推荐答案

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

No need to recreate the animation. Use a CCSpeed action with the CCAnimate action as its inner action. Then you can modify the CCSpeed action's speed property to speed up or slow down the animation.

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

The alternative is to advance frames yourself. It's easy to do, you only need a scheduled update and a timer that tells you when to change to the next frame. You speed up or slow down the animation by simply changing how much you add to the timer every frame. If the timer is > than a specific value you change frames and reset the timer to 0.

这篇关于cocos2d 使用 CCSpeed 动态改变动画速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:cocos2d 使用 CCSpeed 动态改变动画速度

基础教程推荐