eric
eric

Reputation: 4951

How to rotate an SKShapeNode around its own axis?

I want to rotate an SKShapeNode around its axis (a task I assumed would be easy to do in a framework like SpriteKit). The shape rotates, but around an axis that seems to be off screen...

Any ideas on what's going wrong here?

import SpriteKit

class Square: NSObject {

    // this gets created and initialized later...
    var shape: SKShapeNode? = nil

    //… 

    func animateMe(){

        let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
        self.shape.runAction(action, completion: { [unowned self] () -> Void in

            self.shape.removeFromParent()
        })   
    }
}

enter image description here

Upvotes: 5

Views: 863

Answers (1)

jtbandes
jtbandes

Reputation: 118781

Rotation happens around the node's position. So rather than giving the shape node a path with an x/y offset to position your shape, you should change the node's position, and give it a path which is centered around (0,0) so that it draws the shape near its position (because the path is drawn relative to the position as well).

See this answer as well.

Upvotes: 2

Related Questions