0
votes

On SpriteKit, there is a SKAction follow that using for node to follow a path you create with CGMutablePath. Like this one :

path = CGMutablePath()
path?.move(to: point1)
path?.addLine(to: point2)

let follow = SKAction.follow(path!, asOffset: false, orientToPath: true, speed: speedPath!)

But I want to make player to move node free along the path, not some action that follow path. Like there is a path with straight line or ellipse using bezier path and player could move the node along the path. Here is the images :

enter image description here

Any idea about this?

2
How will the player control the movement? By touching the path, in which case the node moves to the touch or by touching anywhere on the screen and the node move to the closest point perhaps? Can the path cross over itself?Steve Ives
player need touch the node and could freely touchmoved the node, but the node only move along the path.willy wijaya

2 Answers

0
votes

You could do something like this (sorry - only pseudo-code for now):

touchesBegan {
   if touch on the node, set flag.
}

touchesMoved {
   if flag not set, return
   if touch is on path {
      Use SKACtion 'follow path' to move node to touch location
   } else {
      stop the node's movement /* Touch has moved away from path */
      clear flag
   }

touchesEnded {
   clear flag
   stop node if moving
}
0
votes

After looking for a while, I finally got what I want. Basically, you need to get each point on the path, whether it is a line or bezier. Then you can move node along the closest points.

Here is the source: Find Nearest point