【问题标题】:SKSpriteNode does not let touches pass through when isUserInteractionEnabled false当 isUserInteractionEnabled false 时,SKSpriteNode 不允许触摸通过
【发布时间】:2016-11-04 06:07:27
【问题描述】:

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

下面是我所说的一个例子:

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 并手动将触摸事件委托给适当的节点,但这会导致很多奇怪的行为。

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

【问题讨论】:

  • 您的 zposition 在场景下方,因此您的 zorder 是覆盖->场景->可触摸

标签: ios swift sprite-kit touch


【解决方案1】:

阅读您找到的实际来源:

/**
  Controls whether or not the node receives touch events
*/
open var isUserInteractionEnabled: Bool

但这是指触摸的内部节点方法。 默认情况下isUserInteractionEnabled 是 false 然后触摸像您的叠加层SKSpriteNode 这样的子对象默认情况下是对主(或父)类处理的简单触摸(对象在这里,存在但如果你不执行任何动作,你只需触摸它)

要通过触摸浏览您的叠加层,您可以在GameScene 中实现如下代码。还要记住不要将-1 用作zPosition,因为这意味着它在您的场景下方。

P.S.:我已经在你的精灵中添加了一个name,以便将其调用到touchesBegan,但是你可以为你的GameScene创建全局变量:

override func sceneDidLoad() {
        // Creat touchable
        let touchable = TouchableSprite(circleOfRadius: 100)
        touchable.zPosition = 0
        touchable.fillColor = SKColor.red
        touchable.isUserInteractionEnabled = true
        touchable.name = "touchable"
        addChild(touchable)
        // Create overlay
        let overlayNode = SKSpriteNode(imageNamed: "fade")
        overlayNode.zPosition = 1
        overlayNode.name = "overlayNode"
        overlayNode.isUserInteractionEnabled = false
        addChild(overlayNode)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("GameScene Touch began")
        for t in touches {
            let touchLocation = t.location(in: self)
            if let overlay = self.childNode(withName: "//overlayNode") as? SKSpriteNode {
                if overlay.contains(touchLocation) {
                    print("overlay touched")
                    if let touchable = self.childNode(withName: "//touchable") as? TouchableSprite {
                        if touchable.contains(touchLocation) {
                            touchable.touchesBegan(touches, with: event)
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    您需要小心使用 zPosition。有可能进入幕后,使事情变得复杂。

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

    现在是这样说的:

    命中测试顺序与绘制顺序相反 在场景中,当 SpriteKit 处理触摸或鼠标事件,它会遍历场景来查找 想要接受事件的最近节点。如果该节点没有 想要事件,SpriteKit 检查下一个最近的节点,依此类推。这 处理命中测试的顺序本质上是相反的 绘制顺序。

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

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

    【讨论】:

    • 这是一个处于最佳状态的人的智慧、轻微的喜剧和尖锐的评论。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多