如何在另一个节点的方向移动节点

How to move node in the direction of another node

我在 Stack overflow 上找到了 答案,但我想对其进行修改,但我无法理解。该问题的答案显示了如何使导弹朝您手指的方向移动。但是,我希望它朝另一个节点(在我的例子中是一艘船)的方向移动,这意味着它不是跟随我的手指,而是跟随船。怎样才能做到这一点?

非常感谢

很简单:

1) 使用 update 方法代替 touchesMoved

2) 使用玩家位置代替触摸位置。

override func update(currentTime: NSTimeInterval) {

        let location = player.position

        //Aim
        let dx = location.x - missile.position.x
        let dy = location.y - missile.position.y
        let angle = atan2(dy, dx)

        missile.zRotation = angle

        //Seek
        let vx = cos(angle) * missileSpeed
        let vy = sin(angle) * missileSpeed

        missile.position.x += vx
        missile.position.y += vy
}