如何禁用对精灵的触摸以使其

如何禁用对精灵的触摸以使其

本文介绍了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 中实现相同的点击"功能?到目前为止,我只看到有人使用Touches started",在这一点上得到要点并在场景中询问节点.

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 属性设置为 NO.因此,默认情况下,对精灵的触摸是点按".

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

因此,对于您想要的叠加层,为精灵创建自定义类,在该类中实现 touchesBegan: 和其他委托,并在初始化时设置 userInteractionEnabled 属性.

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 如何禁用对精灵的触摸以使其“点击"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:56