我查看了无数引用资料以试图理解为什么我的场景没有按照我预期的方式运行,例如 this .
这是我非常简单的 SKScene(2 个子节点):
- 场景有一个 SpriteNode(作为背景图像覆盖整个场景)。这有一个 zPosition = 0。
- 场景有一个第二个节点 (SKNode),它本身有另一个子节点(最多 2 个级别)。这有一个 zPosiiton - 2。
- 所有节点都有 .userInteractionEnabled = false
问题:
当我点击任何地方时,我看到的是第一个 child (SpriteNode) 被触摸了。第二个 child (SKNode) 永远不会被触摸检测到。
请注意,节点的 z 顺序按照我的预期呈现。触摸检测似乎不起作用。
我的 touchesBegan 方法片段:
for touch in touches {
let touchLocation = touch.locationInNode(self)
let sceneTouchPoint = self.convertPointToView(touchLocation)
let touchedNode = self.nodeAtPoint(sceneTouchPoint)
if (touchedNode.name != nil) {
print("Touched = \(touchedNode.name! as String)")
}
}
最佳答案
我有一个类似的问题(背景在 z: 999 + 在 z: <999 中生成“鸭子”节点),我在 Swift 4 中使用以下代码解决了这个问题:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.location(in: self)
let touchedNodes = self.nodes(at: positionInScene)
for touch in touchedNodes {
let touchName = touch.name
if (touchName != nil && touchName!.hasPrefix("pato_")) {
touch.removeFromParent()
}
}
}
关于swift - SpriteKit - 为什么没有检测到 SKNode 的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39494739/