为什么我在 touchesBegan 中没有节点?
Why am I getting no nodes in touchesBegan?
我在 MyBall 的 class 中使用 touchesBegan,它本身就是一个 SKSpriteNode,所以当我点击 MyBall 时它会被调用,并且我确实越过了警戒线。
但是,当我打印 touchedNodes.count 时,我得到零,并且 touchedNodes 确实有一个原始值。
如果有人知道我在这里做错了什么……或遗漏了什么?
p.s。我尝试从 GameScene 本身执行此操作,但从未收到 touchesBegan。
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
}
}
}
当我继续搜索时,我意识到我是在追自己的尾巴。在错误的地方寻找 touchesBegan,还有 GameScene 的 isUserInteractionEnabled = false
,以为我不会碰它。
我终于找到了在一个地方获取所有 touchesBegan 的解决方案,GameScene。
因此,要将所有 touchesBegan 发送到同一场景,请设置:
GameScene 的 isUserInteractionEnabled = true
和所有其他节点 isUserInteractionEnabled = false
Apple 开发者 link 向我解释了这一切:
我在 MyBall 的 class 中使用 touchesBegan,它本身就是一个 SKSpriteNode,所以当我点击 MyBall 时它会被调用,并且我确实越过了警戒线。
但是,当我打印 touchedNodes.count 时,我得到零,并且 touchedNodes 确实有一个原始值。
如果有人知道我在这里做错了什么……或遗漏了什么?
p.s。我尝试从 GameScene 本身执行此操作,但从未收到 touchesBegan。
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
}
}
}
当我继续搜索时,我意识到我是在追自己的尾巴。在错误的地方寻找 touchesBegan,还有 GameScene 的 isUserInteractionEnabled = false
,以为我不会碰它。
我终于找到了在一个地方获取所有 touchesBegan 的解决方案,GameScene。
因此,要将所有 touchesBegan 发送到同一场景,请设置:
GameScene 的 isUserInteractionEnabled = true
和所有其他节点 isUserInteractionEnabled = false
Apple 开发者 link 向我解释了这一切: