Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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?

like image 590
user2202284 Avatar asked Jul 24 '14 21:07

user2202284


1 Answers

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)
like image 85
user2202284 Avatar answered Nov 18 '22 04:11

user2202284