1

I'm trying to add a swipe gesture for multiple nodes at once but it seems to only be working for the first node. When I try and swipe the other nodes it doesn't register there is a swipe. There is only one node but it is copied many times in the .sks file to fill the screen. All the nodes have the same name since it's a copy, I'm hoping to reuse them to fill the screen back up as soon as one of the nodes is swiped off.

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)        
  }
}
alexburtnik
  • 7,661
  • 4
  • 32
  • 70
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92
  • Are you sure that `didMove` is invoked every time you create a scene? – Ryan Oct 26 '16 at 20:13
  • @trick14 I think so, is there a way to be certain – SwiftyJD Oct 26 '16 at 20:34
  • 1
    In short...Adding recognizers to nodes is impossible and that is not what is happening here. You are adding recognizers to the view. If you want node to receive a touch, you should subclass SKSpriteNode and implement touchesBegan. Also you have to set userInteractionEnabled property to true on that object. – Whirlwind Oct 26 '16 at 20:46
  • @Whirlwind I'm still fairly inexperienced with SpriteKit, how can I go about doing something like that – SwiftyJD Oct 27 '16 at 20:23
  • @SwiftyJD There are many answers that cover that topic. For example take a look at these, http://stackoverflow.com/a/27922504/3402095, http://stackoverflow.com/a/36524132/3402095, http://stackoverflow.com/a/26330838/3402095. Really, SO is full of this kind of questions / answers. Just use search ;) – Whirlwind Oct 27 '16 at 20:51

0 Answers0