Sprite-kit: Move an element along a circular path

I am trying to make an element move along the edge of a circle.

  • I created and placed a circle in the center of the screen:
var base = SKShapeNode(circleOfRadius: 200 ) // Size of Circle base.position = CGPointMake(frame.midX, frame.midY) base.strokeColor = SKColor.blackColor() base.glowWidth = 1.0 base.fillColor = UIColor(hue:1, saturation:0.76, brightness:0.94, alpha:1) base.zPosition = 0 self.addChild(base) 
  • Then I created another sprite and added an action to it:
 main = SKSpriteNode(imageNamed:"Spaceship") main.xScale = 0.15 main.yScale = 0.15 main.zPosition = 1 let circle = UIBezierPath(roundedRect: CGRectMake((self.frame.width/2) - 200, CGRectGetMidY(self.frame) - 200,400, 400), cornerRadius: 200) let circularMove = SKAction.followPath(circle.CGPath, duration: 5.0) main.runAction(SKAction.repeatAction(circularMove,count: 2)) self.addChild(main) 

The first rotation (see image below) of the spacecraft goes exactly along the edges of the circle, but the second iteration changes the position of the spacecraft and moves it off the screen. Is this normal or am I doing something wrong?

Thanks.

enter image description here

+9
swift sprite-kit
source share
1 answer

+ follow: duration: produces the same effect as follow: asOffset: orientToPath: duration: with asOffset and orientToPath set to true .

About the asOffset parameter from documents:

If true, the points in the path are relative offsets to node s Start position. If false, the points in the node are absolute coordinates.

So you must explicitly set the value to false:

 let circularMove = SKAction.follow(circle.CGPath, asOffset: false, orientToPath: true, duration: 5) 
+8
source share

All Articles