Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite-kit: Moving an element in circular path

I'm trying make an element move by the edge of circle.

  • I have created and positioned a circle in the middle 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've created another sprite and added an action on 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 spaceship follow exactly the edges of the circle but the second iteration changes the position of the spaceship and move it outside the screen bounds. Is this normal or am I doing something wrong ?

Thanks.

enter image description here

like image 385
Sahbi Belgacem Avatar asked Mar 14 '16 13:03

Sahbi Belgacem


1 Answers

+ follow:duration: produce the same effect as follow:asOffset:orientToPath:duration: with both, asOffset and orientToPath parameters set to true.

About asOffset parameter from the docs:

If true, the points in the path are relative offsets to the node’s starting position. If false, the points in the node are absolute coordinate values.

So, you should explicitly set it to false:

let circularMove = SKAction.follow(circle.CGPath, asOffset: false, orientToPath: true, duration: 5)
like image 177
Whirlwind Avatar answered Oct 23 '22 15:10

Whirlwind