I have a simple rectangular sprite and want to rotate this sprite by 1 degree every time I press a button on screen. I have some simple code I expected to work but it's giving odd results. Instead of rotating the rectangle by 1 degree it seems to be rotating it by at least 180-270 degrees per tap.
var degrees = 0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
let beam = childNode(withName: "beam") as! SKSpriteNode
if let body = physicsWorld.body(at: touchLocation) {
if body.node!.name == "leftTap" {
print("Began touch on Left")
beam.zRotation = CGFloat(degrees)
degrees = degrees + 1
}else{
print("Began touch on right")
}
}
}
In the GameScene.sks sprite attribute inspector you can increment the rotation by pressing the "+" or "-" button. I figured this would work the same way, but I must not be understanding something about the rotation principals here.
any advice would be appreciated.
It's not degrees, it's rads. a value of 3 or so (pi) is a full spin.
https://developer.apple.com/documentation/spritekit/sknode/1483089-zrotation
Here is a handy extension you can use:
extension SKNode {
// Usage is node.rotateBy(45) etc:
func rotateBy(_ degree: CGFloat) {
let conversionFactor = CGFloat(0.01745329252)
self.run(.rotate(byAngle: degree * conversionFactor, duration: 0))
}
}