Iphone 如何在cocos2d中使用动画?

Iphone 如何在cocos2d中使用动画?,iphone,objective-c,cocoa-touch,animation,cocos2d-iphone,Iphone,Objective C,Cocoa Touch,Animation,Cocos2d Iphone,我正在尝试为iPhone开发一款轮盘赌游戏。如何制作轮盘赌板的动画(旋转)?我不知道如何在cocos2d中实现这一点(甚至是什么),但您可以使用CALayers或UIView的核心动画来实现这一点。最简单的方法可能是创建一个包含轮盘赌图像的UIImageView并设置动画 要实现这一点,首先通过使用轮盘赌图像初始化UIImageView来设置它。当您希望控制盘旋转时,请使用以下代码: CATransform3D rotationTransform = CATransform3DMakeRotat

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

我不知道如何在cocos2d中实现这一点(甚至是什么),但您可以使用CALayers或UIView的核心动画来实现这一点。最简单的方法可能是创建一个包含轮盘赌图像的UIImageView并设置动画

要实现这一点,首先通过使用轮盘赌图像初始化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"];
假设rotatingImage是UIImageView

在本例中,车轮将旋转5次,每次旋转耗时0.5秒。旋转被一分为二,因为核心动画将进入下一个最接近的状态,因此,在动画想要向另一个方向旋转之前,最多只能旋转一半。也就是说,圆周率弧度(180度)旋转半圈,但如果旋转角度使用(1.5f*pi),则只能旋转四分之一圈。同样,如果使用(0.999f*pi),则圆将以顺时针方式旋转


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

有几种方法可以做到这一点

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


否则,看看Sprite的RotateBy:方法

在Cocos2D中非常简单:

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

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

如果你需要连续旋转

别忘了确保将sprite transformAnchor设置为图像的中心。我想下一个问题应该出现——如何让它平稳地停止;)


关于动作的更多信息:(这是旧版本的,因此它使用不推荐的“do”而不是“runAction”)

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

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

您可以在下面的链接中找到一些cocos2d动画示例。。您可以在下面的链接中找到一些cocos2d动画示例。看起来角度现在需要以弧度为单位。事实上,挑剔的人用“CCRotateBy”。不过还是要谢谢你。
- (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"];
}