0

i've created an UIBezierPath called curve with following function:

func CreatePath(){
    let startpoint = CGPoint(x: self.frame.size.width / 6.8966, y: self.frame.size.height*0.5)
    let endpoint = CGPoint(x: self.frame.size.width - startpoint.x, y: self.frame.size.height*0.5)
    let controlpoint = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height)
    curve.moveToPoint(startpoint)
    curve.addQuadCurveToPoint(endpoint, controlPoint: controlpoint)
}

It looks (a little bit) like this: enter image description here

Now i have a SKSpriteNode which i want to follow the path in both ways - to the right and to the left. So i've created 2 functions for that.

func moveToTheRight(){
    mySpriteNode.removeActionForKey("moveLeft")
    let moveRight = SKAction.followPath(curve.CGPath, asOffset: false, orientToPath: false, duration: 10)
    mySpriteNode.runAction(moveRight, withKey: "moveRight")
}

func moveToTheLeft(){
    mySpriteNode.removeActionForKey("moveRight")
    let moveLeft = SKAction.followPath(curve.CGPath, asOffset: false, orientToPath: false, duration: 10)
    mySpriteNode.runAction(moveLeft.reversedAction(), withKey: "moveLeft")
}

Now i want that mySpriteNode can change the direction even if the moving action did not finish. For example:

  1. moveToTheRight function was called (mySpriteNode begins from the left and moves to the right end of the path)
  2. 5 seconds have passed
  3. moveToTheLeft function gets called

Right now mySpriteNode "jumps" to the right side of the path and begins there and moves to left. Instead i want that mySpriteNode begins the "movement" from the actual point.

I've tried many things .. i think i have to create a new path inside the moveToTheRight and moveToTheLeft function. So there i could change the startpoint to lets say let startpoint = mySpriteNode.position. But the problem is to create a path with the same curve like the original path - i don't know how i could make a perfect controlpoint with the new startpoint.

Any ideas?

Edit: I guess what i need is a function/formula to get the right green dot value depending on the new startpoint..

enter image description here

aignetti
  • 481
  • 1
  • 3
  • 14
  • you'd have to redefine your path and recreate the action if you want it to continue from where it left off. its going to the beginning of the path because thats what the action and path are telling it to do. – hamobi May 22 '16 at 23:46
  • Hi hamobi. Yes, thats what i thought i have to do. But i don't know how i could create a path with a new starting point with the exact same curve to the end point ... the controlpoint would have to change but it has to be every time to an other position depending on mySpriteNode's position. – aignetti May 23 '16 at 00:04
  • yeah that can be tricky.. this might be tricky to do using SKActions. once you start introducing logic into things they dont work very well. theyre best used for unchanging animations. if you need something to move in a more interactive way you probably need to program your object's movement in your update loop. – hamobi May 23 '16 at 00:06

0 Answers0