Spritekit - 如何一次向多个节点添加滑动手势?

时间:2016-10-26 19:49:24

标签: ios swift sprite-kit uigesturerecognizer skspritenode

我正在尝试同时为多个节点添加滑动手势,但它似乎只适用于第一个节点。当我尝试滑动其他节点时,它没有注册,有一个滑动。只有一个节点,但在.sks文件中多次复制以填充屏幕。所有节点都具有相同的名称,因为它是一个副本,我希望只要其中一个节点被刷掉,就可以重新使用它们来填充屏幕。

let plankName = "woodPlank"

class PlankScene: SKScene {

  var plankWood : SKSpriteNode?
  var plankArray : [SKSpriteNode] = []

  override func didMove(to view: SKView) {        
    plankWood = childNode(withName: "woodPlank") as? SKSpriteNode        
    let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))        
    swipeRight.direction = .right        
    view.addGestureRecognizer(swipeRight)        
  }


  func swipedRight(sender: UISwipeGestureRecognizer) {        
    if sender.direction == .right {          
      let moveOffScreenRight = SKAction.moveTo(x: 400, duration: 0.5)          
      let nodeFinishedMoving = SKAction.removeFromParent()          
      let waitForNode =   SKAction.wait(forDuration: 0.5)                    
      plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving]),
                     completion:{
                  })
      })
    }
  }

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

    print("\(plankWood?.name)")
      if plankWood?.name == plankName {
        print("Plank touched")
    }
  }

  override func enumerateChildNodes(withName name: String, using block: @escaping (SKNode, UnsafeMutablePointer<ObjCBool>) -> Void) {        
    let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight))        
    swipeRight.direction = .right        
    view?.addGestureRecognizer(swipeRight)        
  }
}

0 个答案:

没有答案