Mehul
Mehul

Reputation: 602

How to reverse the direction of path followed by sprite in an SKAction midway?

I have a SKSpriteNode which is moving along a circular path using an SKAction :

  // create the path our sprite will travel along
  let circlePath = CGPathCreateWithEllipseInRect(CGRect(origin: pathCenterPoint, size: CGSize(width: circleDiameter, height: circleDiameter)), nil)

  // create a followPath action for our sprite
  let followCirclePath = SKAction.followPath(circlePath, asOffset: false, orientToPath: false, duration: 2

I can add .ReversedAction() to reverse the direction of the sprite, but that will only happen from the starting point.

How do I reverse the direction of the sprite, when its at some point in the path?

Upvotes: 4

Views: 1048

Answers (1)

Max Kortge
Max Kortge

Reputation: 527

I'm assuming your trying to get it to go in the opposite direction on when the player touches the screen. Try creating a function for both of the directions, one for clockwise and anti-clockwise in these functions add your method of making the path. I would use this code to complete this task as I don't find any errors:

 func moveClockWise() {

    let dx = Person.position.x - self.frame.width / 2
    let dy = Person.position.y - self.frame.height / 2

    let rad = atan2(dy, dx)

    let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 120, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)
    let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 200)
    Person.runAction(SKAction.repeatActionForever(follow).reversedAction())

}

This is just my preferred way, and for anti-clockwise, just create another function just reversing the code.

Now above the didMoveToView add these variables:

var Person = SKSpriteNode()

var Path = UIBezierPath()

var gameStarted = Bool()

var movingClockwise = Bool()

These basically define your Person as a SKSpriteNode() your Path as a UIBezierPath() etc. Of course you would have your Person.position = position and Person = SKSpriteNode(imageNamed: "name") under your didMoveToView to create the sprite.

After this, under the override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {, you want to use the gameStarted bool variable to detect wether it is running, if it is set the bool to true and change it's direction.

if gameStarted == false {

        moveClockWise()
        movingClockwise = true
        gameStarted = true

    }
    else if gameStarted == true {

        if movingClockwise == true {

            moveCounterClockWise()
            movingClockwise = false

        }
        else if movingClockwise == false {

            moveClockWise()
            movingClockwise = true

        }
    }

Basically the first line of code checks whether the bool is false (which it is because nothing has happened to it and it has just loaded), and runs the moveClockwise function and sets the moveClockwise bool to true and the gameStarted bool to true as well. Everything else is pretty self explanatory.

Upvotes: 3

Related Questions