I'm trying to trigger a touch event on a SKSpriteNode where it's child node is tapped on. When the child node it touched, the event isn't triggered. I have found a hack work around using .parent but doesn't feel like the most efficient or elegant way of doing is.

Please see code below:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let touch = touches.first! as UITouch
    let location = touch.locationInNode(self)
    let node = self.nodeAtPoint(location)

    if node is PlanListItem || node.parent is PlanListItem {

        for plan in planListItems as [PlanListItem] {
            plan.selected = false
        }

        // Some more code...
    }
}

Help much appreciated.

score:5

Accepted answer

You can do this inside the node subclass:

class PlanListItem:SKSpriteNode {

    var isSelected: Bool = false
    override init(texture size etc) {
        //your init
        self.userInteractionEnabled = true
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("PlanListItem touched")
        isSelected = !isSelected
    }
}

More questions