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:
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:
- moveToTheRight function was called (mySpriteNode begins from the left and moves to the right end of the path)
- 5 seconds have passed
- 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..