将 userInteractionEnabled 设置为 YES 的精灵在被普通精灵覆盖时不会接收到触摸 [英] Sprite with userInteractionEnabled set to YES does not receive touches when covered by normal sprites

查看:22
本文介绍了将 userInteractionEnabled 设置为 YES 的精灵在被普通精灵覆盖时不会接收到触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我放置了一个精灵 A(子类化以接收命中(userInteractionEnabled 为 YES)),然后放置一个不接受命中的普通精灵 B(userInteractionEnabled 默认为 NO),完全覆盖精灵 A.

I put a sprite A (subclassed to receive hits (userInteractionEnabled to YES)), and then a normal sprite B that does not take hits on top of that (userInteractionEnabled default NO), completely covering sprite A.

点击精灵 B,我假设精灵 A 会得到触摸,但没有任何反应.文档中关于此事的部分如下.

Tapping on sprite B, I assume that sprite A would get the touch but nothing happens. The part from docs about the matter is below.

我觉得这里有些不清楚,因为精灵 B 似乎仍然接收到触摸但将其丢弃.或者,spriteA 已从可能的触摸接收器中移除,因为它不可见.

I feel something is unclear here because it seems still that sprite B receives the touch but throws it away. OR, spriteA is removed from possible touch receiver because it's not visible.

来自文档:https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html#//apple_ref/doc/uid/TP40013043-CH3-SW7

对于在命中测试期间要考虑的节点,其userInteractionEnabled 属性必须设置为 YES.默认值对于除场景节点之外的任何节点都为 NO.想要接收的节点事件需要从其实现适当的响应者方法父类(iOS 上的 UIResponder 和 OS X 上的 NSResponder).这是一您必须在其中实现特定于平台的代码的少数几个地方雪碧套件

For a node to be considered during hit-testing, its userInteractionEnabled property must be set to YES. The default value is NO for any node except a scene node. A node that wants to receive events needs to implement the appropriate responder methods from its parent class (UIResponder on iOS and NSResponder on OS X). This is one of the few places where you must implement platform-specific code in Sprite Kit

无论如何要解决这个问题?只要某些东西的 userInteractionEnabled 为 NO,它就不会干扰其他触摸接收器.

Anyway to fix this? As long as something's userInteractionEnabled is NO, it shouldn't interfere with other touch receivers.

更新:即使将精灵 B 的 alpha 设置为 0.2,使精灵 A 非常可见,也不会使精灵 A 可触摸.尽管没有启用交互,Sprite B 只是完全吞下"了触摸.

Update: Even setting sprite B's alpha to 0.2, making sprite A very visible, will not make sprite A touchable. Sprite B just totally "swallows" the touch, despite being not enabled for interaction.

推荐答案

这是我的解决方案,直到 Apple 以正确的行为更新 SpriteKit,或者有人真正想出如何按照我们想要的方式使用它.

Here is my solution until Apple updates SpriteKit with correct behaviour, or someone acctually figures out how to use it like we want.

https://gist.github.com/bobmoff/7110052

将文件添加到您的项目中并将其导入您的前缀标题中.现在触摸应该按预期方式工作.

Add the file to your project and import it in your Prefix header. Now touches should work the way intended.

这篇关于将 userInteractionEnabled 设置为 YES 的精灵在被普通精灵覆盖时不会接收到触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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