objective-c 如何在 cocos2d 中使用动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/486609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:05:41  来源:igfitidea点击:

how can I use animation in cocos2d?

iphoneobjective-ccocoa-touchanimationcocos2d-iphone

提问by Md Nasir Uddin

I am trying to develop a Roulette game for iPhone. How can I animation (spin) the Roulette board?

我正在尝试为 iPhone 开发轮盘赌游戏。如何动画(旋转)轮盘?

回答by Stanislav

It's quite simple in Cocos2D:

在 Cocos2D 中很简单:

[sprite runAction:[RotateBy actionWithDuration:dur angle:360]];

to make one turn or

转一圈或

id action = [RepeatForever actionWithAction: [RotateBy actionWithDuration:dur angle:360]];
[sprite runAction:action];

and

[sprite stopAction:action];

if you need to rotate continuously.

如果您需要连续旋转。

Don't forget to make sure that sprite transformAnchor is set to center of the image. And I guess next question should arise - how to make it stop smoothly ;)

不要忘记确保精灵transformAnchor 设置为图像的中心。我想应该会出现下一个问题 - 如何让它顺利停止;)

More on actions: http://lethain.com/entry/2008/oct/03/notes-on-cocos2d-iphone-development(it's for older version, so it uses deprecated 'do' instead of 'runAction')

有关操作的更多信息:http: //lethain.com/entry/2008/oct/03/notes-on-cocos2d-iphone-development(它适用于旧版本,因此它使用已弃用的“do”而不是“runAction”)

回答by Brad Larson

I have no idea how to do this in cocos2d (or even what that is), but you can do this using Core Animation either with CALayers or UIViews. Probably the simplest way would be to create a UIImageView containing an image of your roulette wheel and animate that.

我不知道如何在 cocos2d(甚至是什么)中执行此操作,但是您可以使用 Core Animation 和 CALayer 或 UIViews 执行此操作。可能最简单的方法是创建一个包含轮盘图像的 UIImageView 并为其设置动画。

To accomplish this, first set up your UIImageView by initializing it with your roulette wheel image. When you want the wheel to spin, use the following code:

为此,首先通过使用轮盘图像初始化 UIImageView 来设置它。当您希望轮子旋转时,请使用以下代码:

CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);

CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 10; 

[rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

assuming that rotatingImage is your UIImageView.

假设rotatingImage 是你的UIImageView。

In this example, the wheel would rotate 5 times, with each rotation taking 0.5 seconds. The rotations are split in half because Core Animation will go to the next closest state, so the most you can rotate something is a half rotation before the animation wants to rotate in the other direction. That is, the pi radian (180 degree) rotation here goes a half-circle, but if you used (1.5f * pi) for your rotation angle, it would only go a quarter-circle. Likewise, if you used (0.999f * pi), the circle would rotate in a clockwise manner.

在此示例中,轮子将旋转 5 次,每次旋转需要 0.5 秒。旋转被分成两半,因为核心动画会进入下一个最近的状态,所以在动画想要向另一个方向旋转之前,你最多可以旋转半圈。也就是说,这里的 pi 弧度(180 度)旋转是半圆,但如果你使用 (1.5f * pi) 作为旋转角度,它只会是四分之一圆。同样,如果您使用 (0.999f * pi),圆将以顺时针方式旋转。

You'll want to implement acceleration and deceleration of your wheel, and for those a CAKeyframeAnimation would take the place of the CABasicAnimation in this example.

您需要实现滚轮的加速和减速,对于这些,CAKeyframeAnimation 将取代本示例中的 CABasicAnimation。

回答by mahboudz

You can rotate a view, by some number of radians, regardless of whether it is less than a full rotation or many multiples of a full rotation, without having to split the rotation into pieces. As an example, the following code will spin a view, once per second, for a specified number of seconds. You can easily modify it to spin a view by a certain number of rotations, or by some number of radians.

您可以将视图旋转一定数量的弧度,无论它是小于完整旋转还是完整旋转的许多倍,而无需将旋转拆分为多个部分。例如,以下代码将每秒旋转一次视图,持续指定的秒数。您可以轻松修改它以将视图旋转一定数量的旋转或一定数量的弧度。

- (void) runSpinAnimationWithDuration:(CGFloat) duration;
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1.0; 
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

    [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

回答by EightyEight

There are a couple of ways you can do it.

有几种方法可以做到。

If you have the frame-by-frame animation for the wheel, check out AtlasDemo (part of cocos2d distribution).

如果您有轮子的逐帧动画,请查看 AtlasDemo(cocos2d 发行版的一部分)。

Otherwise, take a look at Sprite's RotateBy: method.

否则,请查看 Sprite 的 RotateBy: 方法。