在 SpriteKit 中使用滑动识别器

Using Swipe Recognizer in SpriteKit

我正在尝试在 SpriteKit 中的游戏中实现左右滑动手势,玩家可以收集从屏幕顶部产生的掉落物体。我面临的问题是当手指在屏幕上时试图保持播放器的连续移动,直到触摸结束并且播放器停留在最后一次触摸结束的位置。除了滑动手势之外,可能还有更好的方法来实现这一点,因此我为什么要问你们!任何帮助都会很棒,谢谢大家!

这不是您想要使用滑动(我想您正在使用平移)手势的东西。你要做的是覆盖场景中的touchesBegantouchesMovedtouchesEnded调用,并根据这3种方法计划你的运动。

您可能想要将 SKAction.move(to:duration:) 与这些方法一起使用,并计算出保持恒定速度的数学方法。

例如

func movePlayer(to position:CGPoint)
{
  let distance = player.position.distance(to:position)
  let move = SKAction.move(to:position, duration: distance / 100) // I want my player to move 100 points per second
  //using a key will cancel the last move action
  player.runAction(move,withKey:"playerMoving")
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) 
{
  let touch = touches.first
  let position = touch.location(in node:self)
  movePlayer(to:position)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) 
{
  let touch = touches.first
  let position = touch.location(in node:self)
  movePlayer(to:position)
}


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
  let touch = touches.first
  let position = touch.location(in node:self)
  movePlayer(to:position)
}