I came across this answer, but the answer is not clear to me. Can someone provide some sample code?
Create an SKNode and set its position to the center of rotation. Add the node that should rotate around that center as child to the center node. Set the child node's position to the desired offset (ie radius, say x + 100). Change the rotation property of the center node to make the child node(s) rotate around the center point.
Specifically, "Change the rotation property of the center node" to what?
var centerNode: SKSpriteNode = SKSpriteNode(imageNamed: "image1")
centerNode.position = CGPointMake(self.frame.width/2, self.frame.height/2)
self.addChild(centerNode)
var nodeRotateMe: SKSpriteNode = SKSpriteNode(imageNamged: "image2")
nodeRotateMe.position = CGPointMake(self.frame.width/2 + 100, self.frame.height/2 + 100)
centerNode.addChild(nodeRotateMe)
// Change the rotation property of the center node to what??
centerNode.zRotation = ?
You have a couple of options:
1) You could rotate the
centerNode
over time in yourSKScene
'supdate:
method by changing itszRotation
property manually. You'll have to change the value slowly, at every call toupdate:
to achieve a gradual rotation.Note that the
zRotation
property is in radians, with means a 180 degree rotation would be equal to pi (M_PI
).SKScene
strives toupdate:
at 60 FPS, which means to rotate 360 degrees over 5 seconds, you'd need to increment by 1/300th of a degree every call to update, which would be 1/150th of pi every update.Of course, there is no guarantee that your scene will be able to update at 60 FPS, so you may have to monitor the
currentTime
variable inupdate:
and adjust accordingly.2) Probably better, you could use an SKAction to rotate the
centerNode
for you.Note that the rotation angle is in radians, not degrees. Also note that the
centerNode
need not be a Sprite Node if you don't want to display an image there; it could be a regular SKNode.