在 Swift 中按住按钮移动对象

Move object while holding button in Swift

我有一个 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
           }
      }
   }
}

您可以为此使用 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

            }

         }}

`

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

首先添加一个 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")
}