如何在 cocos2d 中使用动画?

问题描述:

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

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

我不知道如何在 cocos2d 中做到这一点(甚至不知道那是什么),但是您可以使用带有 CALayer 或 UIViews 的 Core Animation 来做到这一点.可能最简单的方法是创建一个包含轮盘图像的 UIImageView 并为其设置动画.

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.

要完成此操作,首先通过使用轮盘图像初始化 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:

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

假设rotatingImage 是你的UIImageView.

assuming that rotatingImage is your UIImageView.

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

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.

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

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.