在精灵套件中,让箭头在击中目标后跟随目标



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

这是我的代码:

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( 会更改箭头的位置以匹配它在目标坐标系中的位置,因此也无需平移箭头。

基本上,这与您在现实生活中的想法完全一样。

当箭头击中目标时,您希望箭头成为该目标的"子项"。 因此,如果你拿起目标,并将其移动到新墙上,箭头将随之而去,因为箭头附着在目标上。

最新更新