Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 Sprite Kit how to disable touches on a sprite to make it "tap through"?

I know that for traditional UIViews, I can set "enable user interaction" flag to NO, and the view will no longer respond to touches, letting the views below them receive touches.

Is there some way to implement the same "tap through" functionality in Sprite Kit? So far I've only seen people using "Touches began", getting the point and asking the scene for nodes at that point.

The problem with this approach is - if I want to add overlays on top of sprites (like monster life points, etc) they will also respond to touches. So far I'm trying to avoid this problem by creating custom classes for different nodes, and then having a very big if statement, checking the class of each node.

Is there a better way to achieve "tap through" functionality for sprite kit?

like image 201
Alex Stone Avatar asked Dec 09 '13 22:12

Alex Stone


2 Answers

If you set the userInteractionEnabled property to YES on a subclassed SKSpriteNode, then the touch delegates will called within the class. Hence, you can handle the touch for the sprite within its class.

However, by default, the userInteractionEnabled property is set to NO. Hence the touch on a sprite is, by default, a "tap through".

So, for the overlays you want, create custom classes for the sprites, implement the touchesBegan: and other delegates within that class and while initialising, set the userInteractionEnabled property to YES.

like image 152
ZeMoon Avatar answered Nov 05 '22 03:11

ZeMoon


Sprite Kit uses the zPosition value only to determine the hit testing and drawing order.

So you could put all your sprites higher than 0 if you don't want to hit them. And then so a simple logic test is zPostion>0

like image 26
Chris Avatar answered Nov 05 '22 03:11

Chris