0
\$\begingroup\$

I am trying to make it so that a SKShapeNode follows a UIBezierPath. Then when the screen is tapped it switches to another UIBezierPath to follow. I have this functionality working except that when the screen is tapped the shape node moves back to where it started. I want it so that when the screen is tapped the circle does not go back to where it starts. Here is my code.

override func didMoveToView(view: SKView) {
    //screenWidth and screenHeight
    screenWidth = view.bounds.width
    screenHeight = view.bounds.height

    // Set up the circle track
    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)

    //Outside track
    outsideTrack = UIBezierPath(roundedRect: CGRectMake((screenWidth/2 - trackWidth/2) - trackWidth*0.08, (screenHeight/2  - trackWidth/2) - trackWidth*0.08, trackWidth + trackWidth*0.16, trackWidth + trackWidth*0.16), cornerRadius: 100)

    //Inside track
    insideTrack = UIBezierPath(roundedRect: CGRectMake((screenWidth/2 - trackWidth/2) + trackWidth*0.08, (screenHeight/2  - trackWidth/2) + trackWidth*0.08, trackWidth - trackWidth*0.16, trackWidth - trackWidth*0.16), cornerRadius: 100)

    //Set the default track
    track = outsideTrack

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

    //Move the circle
    let followPath = SKAction.followPath(track.CGPath, asOffset: false, orientToPath: false, speed: 200.0)
    let repeatForever = SKAction.repeatActionForever(followPath)

    //Move the circle
    circle.runAction(repeatForever)
}

//Movement of circle
func movemementOfCircle(){
    if track == insideTrack{
        track = outsideTrack
    }else{
        track = insideTrack
    }

    //Actions
    circle.removeAllActions()
    //Move the circle
    let followPath = SKAction.followPath(track.CGPath, asOffset: false, orientToPath: false, speed: 200.0)
    let repeatForever = SKAction.repeatActionForever(followPath)

    //Move the circle
    circle.runAction(repeatForever)
}
\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

you need to define two followPath's. e.g.

let followPath1 = SKAction.followPath(track.CGPath, asOffset: false, orientToPath: false, speed: 200.0)
let followPath2 = SKAction.followPath(track.CGPath, asOffset: false, orientToPath: false, speed: 200.0)
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.