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)
Did you look at the SKShapeNode documentation : https://developer.apple.com/library/IOs/documentation/SpriteKit/Reference/SKShapeNode_Ref/index.html
Try using:
shapeNodeWithPath:centered:
Choose YES for centered.
If YES, the path is translated so that the center of the path’s bounding box is at the node’s origin; otherwise the path is relative to the node’s origin.
SKSpriteNode rectangle = [SKSpriteNode spriteWithSize size: CGSizeMake(20,20)];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With