如何在 cocos2d 中使用动画? [英] how can I use animation in cocos2d?

查看:29
本文介绍了如何在 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.

这篇关于如何在 cocos2d 中使用动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆