SpriteKit:如何使 touchesBegan 对不在最高 zPosition 上的节点起作用

问题描述

我正在制作一个 SpriteKit 游戏,它的按钮节点位于最高 zPosition。我正在覆盖 touchesBegan 函数以向按钮添加功能。它起作用是因为该按钮位于 z 维度的最高位置。 .zPosition 设置为与任何其他节点相比的最高值。我需要在场景中有另一个按钮,但它需要具有较低的 .zPosition,因为它需要在某一点被另一个节点覆盖。当我的 .zPosition 低于另一个节点时,touchesBegan 方法不会触发。如果我有更高的 .zPosition ,它会起作用,但这会使其他按钮节点不起作用。无论 .zPosition 是什么,我怎样才能使 touchesBegan 方法工作?请向我询问更多说明。谢谢!

解决方法

在 touchesBegan 中获得点后,您是否尝试测试按钮节点是否包含该点:

func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) -> Bool {
    if touches.count == 1 {
        if let touch = touches.first {
            let location = touch.location(in: self)
            if myButtonNode.contains(location) {
...
,

您想使用 nodes(at:) 方法。这是在特定点的所有节点的数组,该点是您触摸的位置。

override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {

for t in touches {
  let location = t.location(in: self)
  let touchedNodes = self.nodes(at: location) // Array of all nodes at the point
  
  for node in touchedNodes { // Iterate through all nodes in the touchedNode array
    if node.name == "your button node" {
    ... // Add your button code here
    }
  }
}
    
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...