iOS7 Sprite Kit如何禁用精灵上的触摸以使其“轻触”? [英] iOS7 Sprite Kit how to disable touches on a sprite to make it "tap through"?

查看:124
本文介绍了iOS7 Sprite Kit如何禁用精灵上的触摸以使其“轻触”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对于传统的UIViews,我可以将启用用户交互标记设置为NO,并且视图将不再响应触摸,让它们下面的视图接收到触摸。

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.

有没有办法在Sprite Kit中实现相同的点击功能?到目前为止,我只看到人们使用触摸开始,获得重点并在那时向场景询问节点。

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.

这种方法的问题是 - 如果我想在精灵之上添加叠加(如怪物生命点等),他们也会对触摸做出反应。到目前为止,我试图通过为不同的节点创建自定义类来避免这个问题,然后使用非常大的if语句,检查每个节点的类。

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.

有没有更好的方法来实现sprite kit的点击功能?

推荐答案

如果你设置了在子类SKSpriteNode上的 userInteractionEnabled 属性为 YES ,然后触摸委托将在类中调用。因此,您可以在其类中处理精灵的触摸。

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.

但是,默认情况下, userInteractionEnabled 属性设置为。因此,默认情况下,精灵上的触摸是点击。

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

因此,对于所需的叠加层,为精灵创建自定义类,实现 touchesBegan:以及该类中的其他代表,在初始化时,将 userInteractionEnabled 属性设置为 YES

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.

这篇关于iOS7 Sprite Kit如何禁用精灵上的触摸以使其“轻触”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆