问题描述
我正在制作一个 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
}
}
}
}