Michael Matta
Michael Matta

Reputation: 23

Swift SpriteKit turning sprite to certain angle

In Swift SpriteKit I have the "angle" of a sprite stored in an integer. It starts at zero and adds or subtracts 45 whenever the user rotates left or right (0-360), how would I create an SKAction to rotate the sprite to the stored angle? (in the case that the angle gets shifted unintentionally)

Upvotes: 1

Views: 592

Answers (2)

user3069232
user3069232

Reputation: 8995

iOS 11.x Swift 4.2 a translation of Clever Error answer

let radAngle = CGFloat(10) * .pi / 180
let rotationAction = SKAction.rotate(toAngle: radAngle, duration: 0.5)
floor.run(rotationAction)

Upvotes: 1

Craig Siemens
Craig Siemens

Reputation: 13266

iOS generally uses radians to describe angles. You'll need to convert your angle in degrees to radians before you can create the action.

let radAngle = CGFloat(angle) * .pi / 180
let rotationAction = SKAction.rotateToAngle(radAngle, duration: 0.5)
node.runAction(rotationAction)

Upvotes: 3

Related Questions