What you are trying was working on iOS8. But that has been fixed in iOS9. If you try your code on iOS8 you will see that it works. This is one of the things that annoyed me on iOS8, because it doesn't make sense that you can tap on a hidden node. Still, it would be nice that you can tap a node with alpha 0.0 (works on iOS8 too), but this has been fixed as well in iOS9. So setting the alpha to 0.0 will not work.
The Solution
You can use SKNode's containsPoint method:
Returns a Boolean value that indicates whether a point lies inside the
parent’s coordinate system.
Parent in your case would be your sprite. Read here how containsPoint
method works. Basically, containsPoint
doesn't care if the node is visible or not, or does it have a parent or not. It just checks if a certain point (location
) lies inside of parent's (touchedNode
) coordinate system. So here is how you can do it using this method:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let location = touches.first!.locationInNode(self)
self.enumerateChildNodesWithName("pieces") { node, stop in
if node.containsPoint(location) && node.hidden == true {
node.hidden = false
stop.memory = true
}
}
}
Here, you are enumerating through all of the pieces (I assumed that self is their parent), and if certain node contains touch location plus it is hidden, then make it visible (and stop the further, unnecessary search).
HINT:
Also consider using optional binding here:
if let touch = touches.first {
let location = touch.locationInNode(self)
//do your stuff here
}
It is good habit to use optional binding because it is safer.