iOS7 Sprite Kit how to disable touches on a sprite

2020-03-17 13:47发布

问题:

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?

回答1:

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.



回答2:

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