在Swift中按住按钮移动对象

时间:2017-09-20 16:46:01

标签: swift xcode sprite-kit skspritenode touchesbegan

我有一个小的太空入侵者游戏,我添加了两个按钮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
           }
      }
   }
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

你可以使用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")

}
     }

UPDATE 你编写按钮的方式非常奇怪,我这样做,它总是有效:`

 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

            }

         }}

`