ios 如何在精灵套件中旋转精灵节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19390064/
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-08-31 02:21:12  来源:igfitidea点击:

How to rotate a sprite node in sprite kit?

iossprite-kitrotation

提问by Vince

I am making a game and I'm having troubles with rotating a sprite node, This is the code I have; What do I have to add to turn it, let's say 45 degrees?.

我正在制作游戏,但在旋转精灵节点时遇到问题,这是我的代码;我必须添加什么才能转动它,比如说 45 度?

SKSpriteNode *platform = [SKSpriteNode spriteNodeWithImageNamed:@"YellowPlatform.png"];
platform.position = CGPointMake(CGRectGetMidX(self.frame), -200+CGRectGetMidY(self.frame));
platform.size = CGSizeMake(180, 10);
platform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:platform.size];
platform.physicsBody.dynamic = NO;
[self addChild:platform];

采纳答案by BSevo

You make SKAction for the rotation and run that action on the node. for example:

您为轮换制作 SKAction 并在节点上运行该操作。例如:

//M_PI/4.0 is 45 degrees, you can make duration different from 0 if you want to show the rotation, if it is 0 it will rotate instantly
SKAction *rotation = [SKAction rotateByAngle: M_PI/4.0 duration:0]; 
//and just run the action
[yourNode runAction: rotation];  

回答by quaertym

Just do this without using actions:

只需执行此操作而不使用操作:

sprite.zRotation = M_PI / 4.0f;

And in Swift 4:

在 Swift 4 中:

sprite.zRotation = .pi / 4

回答by ZeMoon

Using the predefined value will be faster:

使用预定义值会更快:

sprite.zRotation = M_PI_4;

These predefined constants in math.h are available as literals and can be used to reduce actual processing of values like M_PI / 4.0

math.h 中的这些预定义常量可用作文字,可用于减少对 M_PI / 4.0 等值的实际处理

回答by J. V.

sprite.zRotation = M_PI_4;

and

[sprite runAction:[SKAction rotateByAngle:M_PI_4 duration:0]];

are not the same.

不一样。

running the SKAction will animate the change even if the duration is 0 it will do it over very short time. changing the zRotation will show it in the new rotation.

即使持续时间为 0,运行 SKAction 也会为更改设置动画,它会在很短的时间内完成。更改 zRotation 将在新的旋转中显示它。

why this is important: if you add new sprite doing on it [SKAction rotateByAngle:] will create flickering/ugliness in the sprite as it comes on the view.

为什么这很重要:如果你在它上面添加新的精灵 [SKAction rotateByAngle:] 会在精灵出现在视图中时产生闪烁/丑陋。

if the sprite is on the screen and you want to rotate it changing the zRotation will not be as nice as rotatebyAngle: even with 0 duration.

如果精灵在屏幕上并且您想旋转它,则更改 zRotation 不会像 rotatebyAngle 那样好:即使持续时间为 0。

回答by Dave Levy

In Swift 4 the way I got mine to rotate was

在 Swift 4 中,我让我的旋转方式是

sprite.zRotation =  .pi / 2 // 90 degrees
sprite.zRotation =  .pi / 4 // 45 degrees

回答by wm.p1us

For SWIFT 3 / SWIFT 4version you could use next:

对于SWIFT 3 / SWIFT 4版本,您可以使用下一个:

sprite.zRotation = .pi / 4

or if you like actions:

或者如果你喜欢行动:

let rotateAction = SKAction.rotate(toAngle: .pi / 4, duration: 0)
sprite.run(rotateAction)

回答by Chigo Godwin Anyaso

//rotates player node 90 degrees
player.zRotation = CGFloat(M_PI_2)*3;
//rotates 180 degrees
player.zRotation = CGFloat(M_PI_2)*2;
//rotates 270 degrees
player.zRotation = CGFloat(M_PI_2)*1;
//rotates 360 degrees (where it was originally)
player.zRotation = CGFloat(M_PI_2)*4;
//rotates 315 degrees
player.zRotation = (CGFloat(M_PI_2)*1)/2;
and so on...