在 Swift 中限制节点沿路径移动

Restrict node movement along path in Swift

在 SpriteKit 上,有一个 SKAction follow 用于节点跟随您使用 CGMutablePath 创建的路径。喜欢这个:

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

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

但我想让玩家沿着路径自由移动节点,而不是跟随路径的一些动作。就像有一条使用贝塞尔曲线的直线或椭圆路径,玩家可以沿着路径移动节点。这是图片:

对此有什么想法吗?

你可以这样做(抱歉 - 目前只有伪代码):

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
}

找了半天,终于如愿以偿了。 基本上,您需要获取路径上的每个点,无论是直线还是贝塞尔曲线。 然后你可以沿着最近的点移动节点。

来源如下: Find Nearest point