objective-c - 如何让*转起来?
伊谢尔伦
伊谢尔伦 2017-04-22 08:59:23
0
1
521

我想开发一款iPhone上的*游戏,怎样可以让这个*转起来呢?

原问题:how can I use animation in cocos2d?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
洪涛

Answer: Brad Larson
(Best answer)
I don't know how to insert animations on cocos2d, but you can create core animations with CALayers or UIViews. Perhaps the simplest way is to create a UIImageView containing * patterns and then animate it.
First, create a new UIImageView and set the * pattern to its initial value. If you want the * to rotate, 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"];

Assume rotatingImage is the UIImageView you need.
In this example, the turntable will rotate 5 times, each time taking 0.5 seconds. The rotation will stop at half a circle, because Core Animation changes direction at half a circle, so before the rotation changes direction, at most Let the turntable turn half a turn. In other words, if you set the rotation angle to (1.5f * pi), it can only rotate ninety degrees. If you set the radian value to (0.999f * pi), the turntable will rotate clockwise.
If you want to achieve acceleration or deceleration effects, it is best not to use CABasicAnimation. CAKeyframeAnimation will have better effects.


Answer: Stanislav
In cocos2d, an easy way is: set

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

Complete the rotation action. If you need continuous rotation, you can set it

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

and

[sprite stopAction:action];

Before this, be sure to set the sprite transformAnchor so that the image is centered.


Answer: mahboudz
You can set different arc parameters to complete a complete rotation, without having to consider whether it can complete a complete rotation, and there is no need to divide the entire rotation process into several parts to design. For example, the following code can complete the rotation effect, rotating once per second:

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

Answer: EightyEight
There are many ways to achieve animation effects. If you want to set a frame-by-frame mode animation effect for the turntable, you can use AtlasDemo to achieve this goal.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template