当 isUserInteractionEnabled false 时,SKSpriteNode 不允许触摸通过 [英] SKSpriteNode does not let touches pass through when isUserInteractionEnabled false

查看:30
本文介绍了当 isUserInteractionEnabled false 时,SKSpriteNode 不允许触摸通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SKSpriteNode 在 SpriteKit 中创建一个叠加层.但是,我希望触摸通过覆盖层,因此我将 isUserInteractionEnabled 设置为 false.然而,当我这样做时,SKSpriteNode 似乎仍然吸收了所有的接触,并且不允许底层节点做任何事情.

I'm attempting to create an overlay in SpriteKit which by using an SKSpriteNode. However, I want the touches to pass through the overlay, so I set isUserInteractionEnabled to false. However, when I do this, the SKSpriteNode still seems to absorb all the touches and does not allow the underlying nodes to do anything.

以下是我正在谈论的示例:

Here's an example of what I'm talking about:

class 

TouchableSprite: SKShapeNode {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("Touch began")
    }
}

class GameScene: SKScene {
    override func sceneDidLoad() {
        // Creat touchable
        let touchable = TouchableSprite(circleOfRadius: 100)
        touchable.zPosition = -1
        touchable.fillColor = SKColor.red
        touchable.isUserInteractionEnabled = true
        addChild(touchable)


        // Create overlay
        let overlayNode = SKSpriteNode(imageNamed: "Fade")
        overlayNode.zPosition = 1
        overlayNode.isUserInteractionEnabled = false
        addChild(overlayNode)
    }
}

我尝试将 SKSpriteNode 子类化并手动将触摸事件委托给适当的节点,但这会导致许多奇怪的行为.

I've tried subclassing SKSpriteNode and manually delegating the touch events to the appropriate nodes, but that results in a lot of weird behavior.

任何帮助将不胜感激.谢谢!

Any help would be much appreciated. Thanks!

推荐答案

你需要小心 zPosition.有可能走到幕后,使事情变得复杂.

You need to be careful with zPosition. It is possible to go behind the scene, making things complicated.

更糟糕的是触摸事件的顺序.我记得读过它基于节点树,而不是 zPosition.禁用场景触摸,您应该会看到结果.

What is worse is the order of touch events. I remember reading that it is based on the node tree, not the zPosition. Disable the scene touch and you should be seeing results.

现在是这样的:

命中测试顺序与绘制顺序相反 在场景中,当SpriteKit 处理触摸或鼠标事件,它在场景中走动寻找想要接受事件的最近节点.如果那个节点没有想要事件,SpriteKit 检查下一个最近的节点,依此类推.这处理命中测试的顺序基本上与绘制顺序.

The Hit-Testing Order Is the Reverse of Drawing Order In a scene, when SpriteKit processes touch or mouse events, it walks the scene to find the closest node that wants to accept the event. If that node doesn’t want the event, SpriteKit checks the next closest node, and so on. The order in which hit-testing is processed is essentially the reverse of drawing order.

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

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 SpriteKit.

但情况可能并非总是如此,因此您需要检查 8 、 9 和 10 之间的一致性

But this may not have always been the case, so you will need to check between 8 , 9, and 10 for consistency

这篇关于当 isUserInteractionEnabled false 时,SKSpriteNode 不允许触摸通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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