Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 230
eric Avatar asked Oct 31 '22 19:10

eric


1 Answers

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.

like image 138
jtbandes Avatar answered Nov 14 '22 16:11

jtbandes