0
votes

I am trying to move a SKShapeNode along a UIBezierPath. Here is the code I have so far:

// Set up the circle track
    let circleTrack = UIBezierPath(roundedRect: CGRectMake(screenWidth/2, screenHeight/2, screenWidth/3, screenWidth/3), cornerRadius: 100)
    let shapeTrack = SKShapeNode(path: circleTrack.CGPath, centered: true)
    shapeTrack.position = CGPointMake(screenWidth/2, screenHeight/2)
    shapeTrack.strokeColor = SKColor.whiteColor()
    self.addChild(shapeTrack)

    // Create the ball
    let circle = SKShapeNode(circleOfRadius: 15)
    circle.position = CGPointMake(screenWidth/2, screenHeight/2)
    circle.fillColor = SKColor.whiteColor()
    self.addChild(circle)

    //Move the circle
    circle.runAction(SKAction.repeatActionForever(SKAction.followPath(circleTrack.CGPath, speed: 3.0)))

All the action does is make the shape node disappear. How can I get it so that the circle moves along the bezier path forever?

1

1 Answers

1
votes

You are placing the bezier path circleTrack in one position, and the shapeTrack in another position. The origin of circleTrack is at (screenWidth/2,screenHeight/2) and the shapeTrack is centered at (screenWidth/2,screenHeight/2). shapeTrack.position is the position of the center of the SKShapeNode. Try the following code.

let screenWidth = size.width
let screenHeight = size.height

let trackWidth = screenWidth/3

let circleTrack = UIBezierPath(roundedRect: CGRectMake(screenWidth/2 - trackWidth/2, screenHeight/2  - trackWidth/2, trackWidth, trackWidth), cornerRadius: 100)
let shapeTrack = SKShapeNode(path: circleTrack.CGPath, centered: true)
shapeTrack.position = CGPointMake(screenWidth/2, screenHeight/2)
shapeTrack.strokeColor = UIColor.whiteColor()
self.addChild(shapeTrack)

// Create the ball
let circle = SKShapeNode(circleOfRadius: 15)
circle.fillColor = UIColor.whiteColor()
self.addChild(circle)

let followPath = SKAction.followPath(circleTrack.CGPath, asOffset: false, orientToPath: false, speed: 200.0)

//Move the circle
circle.runAction(SKAction.repeatActionForever(followPath))