I have an SKShapeNode (rectangle in this case) which I am trying to rotate along its center. However it is rotating along the screen's bottom left point. Since I cannot set an anchor point for SKShapeNode, how can I achieve my requirement (to rotate the shape along its centre). This is the code I'm trying.
let rectangle = SKShapeNode()
rectangle.path = UIBezierPath(rect: CGRectMake(view.frame.width/4, view.frame.height/2, view.frame.width/2, view.frame.width/2)).CGPath
rectangle.fillColor = UIColor.yellowColor()
rectangle.strokeColor = UIColor.yellowColor()
let oneRevolution = SKAction.rotateByAngle(360, duration: 100.0)
let repeat = SKAction.repeatActionForever(oneRevolution)
rectangle.runAction(repeat)
When you create the SKShapeNode you have to set it to center this way:
That way the anchor point of the SKShapeNode will be the center point.
We have to explicitly center the SKShapeNode before applying rotation.
In the first and second examples below, this is done by setting
x
andy
parameters while initializing the rectangle, and then setting the position property.In the third example below, this is done by setting
centered
totrue
while initializing the shape, and then setting the position property.You can make the same rectangle as a spriteNode which would give you access to change the anchorpoint. My syntax may be off a bit but I believe it can also take a color in the constructor.
Did you look at the SKShapeNode documentation : https://developer.apple.com/library/IOs/documentation/SpriteKit/Reference/SKShapeNode_Ref/index.html
Try using:
Choose YES for centered.