在 SpriteKit 中,使箭头在击中目标后跟随目标

In SpriteKit, make arrow follow target once it hit it

我的游戏有一个从右向左移动的目标,一旦箭射中它,它们应该一起从右向左移动。我能够让它工作,但它并不完美——箭头在目标中有点晃动,有时它会离开屏幕。我认为这与帧速率有关,但我不确定如何解决它。

这是我的代码:

func arrowCollideWithTarget() {
    arrows.last!.physicsBody?.velocity = CGVector(dx: 0, dy: 0)

    let followAction = SKAction.customAction(withDuration: TimeInterval(Int.max)) {
        node, elapsedTime in

        node.position.x += (self.targetLocation?.x)! - (self.latestTargetLocation?.x)!
    }

    arrows.last!.run(followAction)

}

非常简单的解决方案,move(toParent)

arrows.last!.move(toParent:target)

现在不需要任何操作,move(toParent) 会更改箭头的位置以匹配它在目标坐标系中的位置,因此也不需要平移箭头。

基本上,这完全符合您在现实生活中的想法。

当箭头击中目标时,您希望箭头成为该目标的 "child"。所以如果你拿了目标,把它移到一堵新墙上,箭头就会跟着它移动,因为箭头是附着在目标上的。