如何在sprite kit中旋转精灵节点?

Vin*_*nce 22 rotation ios sprite-kit

我正在制作游戏,我在旋转精灵节点时遇到了麻烦,这就是我的代码; 我需要添加什么来转动它,比方说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];
Run Code Online (Sandbox Code Playgroud)

qua*_*tym 67

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

sprite.zRotation = M_PI / 4.0f;
Run Code Online (Sandbox Code Playgroud)

在Swift 4中:

sprite.zRotation = .pi / 4
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案.只需旋转精灵就不需要运行零持续时间的动作. (3认同)

小智 19

您为旋转进行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];  
Run Code Online (Sandbox Code Playgroud)


ZeM*_*oon 14

使用预定义的值会更快:

sprite.zRotation = M_PI_4;
Run Code Online (Sandbox Code Playgroud)

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

  • 废话。它将产生相同的编译代码并花费相同的时间(差异将是不可估量的)。话虽如此,没有理由不使用`M_PI_4`。 (2认同)

小智 14

sprite.zRotation = M_PI_4;
Run Code Online (Sandbox Code Playgroud)

[sprite runAction:[SKAction rotateByAngle:M_PI_4 duration:0]];
Run Code Online (Sandbox Code Playgroud)

不一样.

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

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

如果精灵在屏幕上并且你想要旋转它,改变zRotation将不如rotatebyAngle那样好:即使持续时间为0.


Dav*_*evy 6

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

sprite.zRotation =  .pi / 2 // 90 degrees
sprite.zRotation =  .pi / 4 // 45 degrees
Run Code Online (Sandbox Code Playgroud)


wm.*_*1us 5

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

sprite.zRotation = .pi / 4
Run Code Online (Sandbox Code Playgroud)

或者如果你喜欢行动:

let rotateAction = SKAction.rotate(toAngle: .pi / 4, duration: 0)
sprite.run(rotateAction)
Run Code Online (Sandbox Code Playgroud)