为什么我在触摸中没有节点Began



我在MyBall的类中使用touchesBegan,MyBall本身就是一个SKSpriteNode,所以当我点击MyBall时,它会被调用,我确实越过了警戒线。

然而,当我打印touchedNodes.count时,我得到零,而touchedNode确实有一个原始值。

如果有人知道我在这里做错了什么。。。。还是失踪了?

p.s.我试着从GameScene本身做这件事,但触摸Began从未收到。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }

let location = touch.location(in: self)
let touchedNodes = self.nodes(at: location)
print ("(touchedNodes.count)")

//combine next two lines
for node in touchedNodes.reversed(){
var thisBall = node as? MyBall
if thisBall!.nodeType == "ball" {
self.currentNode = node
}
}
}

当我继续搜索时,我意识到我在追逐自己的尾巴。寻找触摸开始在错误的地方,以及有GameScene的isUserInteractionEnabled = false,认为我不会触摸它。

最后,我找到了一个解决方案,让所有的触摸在一个地方开始,游戏场景。

因此,要将所有触摸Began发送到同一场景,请设置:GameScene的isUserInteractionEnabled = true以及所有其他节点到isUserInteractionEnabled = false

这是苹果开发者的链接,它向我解释了这一切:

控制节点上的用户交互

最新更新