swiftsprite-kitzposition

SpriteKit - Why SKNode's are not being touch detected


I have reviewed countless references to try to understand why my scene is not behaving the way i expected it to, such as this.

Here is my very simple SKScene (2 child nodes):

Issue:

When i click anywhere all i see is that the 1st child (SpriteNode) is touched. The 2nd child (SKNode) is never touch-detected.

Note that the z-ordering of the Nodes are being rendered as I expect them. It is the touch-detection that doesnt appear to be working.

Snippet of my touchesBegan method:

       for touch in touches {
            let touchLocation = touch.locationInNode(self)
            let sceneTouchPoint = self.convertPointToView(touchLocation)
            let touchedNode = self.nodeAtPoint(sceneTouchPoint)
            if (touchedNode.name != nil) {
                print("Touched = \(touchedNode.name! as String)")
            }
        }

Solution

  • I had a similar issue (background in z: 999 + spawning "ducks" nodes in z: <999) that I solved with the following code in Swift 4:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch:UITouch = touches.first!
        let positionInScene = touch.location(in: self)
        let touchedNodes = self.nodes(at: positionInScene)
        for touch in touchedNodes {
            let touchName = touch.name
            if (touchName != nil && touchName!.hasPrefix("pato_")) {
                touch.removeFromParent()
            }
        }
    }