Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating an SKShapeNode along its center

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)

enter image description here

like image 310
Bijoy Thangaraj Avatar asked Oct 09 '14 17:10

Bijoy Thangaraj


2 Answers

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.

like image 68
Rahul Iyer Avatar answered Oct 03 '22 07:10

Rahul Iyer


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.

like image 38
meisenman Avatar answered Oct 03 '22 06:10

meisenman