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)
小智 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等值的实际处理
小智 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.
在 Swift 4 中,我让我的旋转方式是
sprite.zRotation = .pi / 2 // 90 degrees
sprite.zRotation = .pi / 4 // 45 degrees
Run Code Online (Sandbox Code Playgroud)
对于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)
归档时间: |
|
查看次数: |
36261 次 |
最近记录: |