user2202284
user2202284

Reputation: 291

Using swift and spriteKit how can I rotate SKSpriteNode around the point outside of node?

How can I rotate a Node in SpriteKit around an arbitrary point?

I have already googled a answer "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. The same works for cocos2d btw."

But I really dont understand how to write this in code :(. Im very beginner in swift and this looks quite difficult. Can anyone give me a help with code?

Upvotes: 7

Views: 5364

Answers (1)

user2202284
user2202284

Reputation: 291

I did it this way after a few hours of experimenting.

    var fish: SKSpriteNode = SKSpriteNode(imageNamed: "fish")

    fish.anchorPoint = CGPoint(x:CGFloat(-0.5),y:CGFloat(-0.2)) 
    fish.zPosition = CGFloat(y+0.5)  
    fish.position = CGPoint(x:CGFloat(x),y:CGFloat(-60-96*(y-3)))

    var jump = SKAction.rotateToAngle(CGFloat(3.14), duration: NSTimeInterval(1))
    fish.runAction(jump)

Upvotes: 9

Related Questions