【问题标题】:Move object while holding button in Swift在 Swift 中按住按钮移动对象
【发布时间】:2017-09-20 16:46:00
【问题描述】:

我有一个小型 Space Invaders 游戏,我添加了两个按钮 leftButtonrightButton,我希望 ship 移动,只要其中一个按钮被触摸并保持在正确的方向。

我让它朝正确的方向移动,但我必须反复点击按钮,但我希​​望ship 在玩家按住按钮时移动。

为简单起见,我只发布了左侧按钮的代码,因为我相信其他按钮的编码方式是相同的:

class GameScene: SKScene {
   var leftButton = SKSpriteNode()
   var ship = SKSpriteNode()

   override func didMove(to view: SKView) {
       leftButton = self.childNode(withName: "left") as! SKSpriteNode
       ship = self.childNode(withName: "player") as! SKSpriteNode
   }

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       for touch: AnyObject in touches {
           let pointTouched = touch.location(in: self)

           if leftButton.contains(pointTouched) {
               player.position.x -= 30
           }
      }
   }
}

【问题讨论】:

  • 对于不断移动使用更新:方法并直接更改位置属性(不使用操作)。
  • 或者如果你使用物理,在提到的方法中对节点的物理体施加力。
  • 如果您不想弄乱您的更新方法,那么您可以在 touchdown 事件上使用一个动作,并使用 touch up 事件删除该动作。只需确保使用 moveTo,并使用您所在位置到边缘的距离来确定时间。 (如果遍历整个屏幕需要 5 秒,那么您应该需要 2.5 秒)
  • 对不起,我不明白如何实现更新的方法!

标签: swift xcode sprite-kit skspritenode touchesbegan


【解决方案1】:

为此使用一个动作。还有其他方法可以实现这一点,但这对你有用。

首先添加一个func moveBy,该func moveBy代表您希望您的船行驶的方向,以及一个key: String。稍后您将在 touchesBegan 中使用这些。

func moveShip (moveBy: CGFloat, forTheKey: String) {
    let moveAction = SKAction.moveBy(x: moveBy, y: 0, duration: 1)
    let repeatForEver = SKAction.repeatForever(moveAction)
    let seq = SKAction.sequence([moveAction, repeatForEver])

    //run the action on your ship
    player.run(seq, withKey: forTheKey)
}

TouchesBegan:

        if leftButton.contains(pointTouched) {
            moveShip(moveBy: -30, forTheKey: "left")
        }

        if rightButton.contains(pointTouched) {
            moveShip(moveBy: 30, forTheKey: "right")
        }

编辑:

touchesEnded:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    player.removeAction(forKey: "left")
    player.removeAction(forKey: "right")
}

【讨论】:

  • 这确实有效,非常感谢!我只需要在 touchesEnded 中添加一个 removeAction。也许您可以将其编辑为您的答案,所以每个人都知道!
【解决方案2】:

您可以为此使用 SKAction:`

class GameScene: SKScene {
  var leftButton = SKSpriteNode()
  var ship = SKSpriteNode()
       let Left = SKAction.repeatForever(
        SKAction.sequence([
            SKAction.wait(forDuration: 0.5),
            SKAction.run({
              SKAction.move(by: CGVector(dx: -30, dy: 0), duration:0.5)
            }  )]))
 //This SKAction will repeat it self forever, until the action is removed. It's build from a sequence of to other actions, move and wait.
  override func didMove(to view: SKView) {
      leftButton = self.childNode(withName: "left") as! SKSpriteNode
     ship = self.childNode(withName: "player") as! SKSpriteNode
   }

  override func touchesBegan(_ touches: Set<UITouch>, with event:UIEvent?) {
      for touch: AnyObject in touches {
          let pointTouched = touch.location(in: self)

          if leftButton.contains(pointTouched) {
         self.run(Left, withKey: "left")
    //Keys will help you remove actions, as well as to remove certain actions, sorting them by group
          }
    }
 }
     override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)   {
    self.removeAction(forKey: "left")

}
     }

更新 你编码按钮的方式真的很奇怪,我这样做而且它总是有效:`

 if let touch = touches.first{
          let pos = touch.location(in: self)
          let node = self.atPoint(pos)
          if node == button {

              if let view = view {

                   //your code

            }

         }}

`

【讨论】:

  • 因为这对我来说绝对有意义,而且我确实这样做了,当我按下左键时,它仍然没有移动船。我只是看不到按钮是如何通过动作连接到船的。无论如何,谢谢你,很难找到任何最新的 swift 答案!