繁体   English   中英

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

[英]How to rotate a sprite node in 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];

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

sprite.zRotation = M_PI / 4.0f;

在Swift 4中:

sprite.zRotation = .pi / 4

您为旋转进行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];  

使用预定义的值会更快:

sprite.zRotation = M_PI_4;

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

sprite.zRotation = M_PI_4;

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

不一样。

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

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

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

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

sprite.zRotation = .pi / 4

或者如果你喜欢行动:

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

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

sprite.zRotation =  .pi / 2 // 90 degrees
sprite.zRotation =  .pi / 4 // 45 degrees
//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...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM