swift - 为什么我在 touchesBegan 中没有节点?
问题描述
我在 MyBall 类中使用 touchesBegan,它本身就是一个 SKSpriteNode,所以当我点击 MyBall 时它会被调用,并且我确实越过了警戒线。
但是,当我打印touchNodes.count 时,我得到零,并且touchNodes 确实有一个原始值。
如果有人知道我在这里做错了什么......或失踪?
ps 我尝试从 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'sisUserInteractionEnabled = true
和所有其他节点到isUserInteractionEnabled = false
这是 Apple Developer 的链接,它向我解释了这一切:
推荐阅读
- user-interface - 在球拍中按下时更改按钮的图像
- c - 使用 C 中的数组计算直角三角形的斜边
- sed - sed 找到带有集合文章的行 (a, an, the)
- c - void* 数组 C 的冒泡排序
- python - 直接将URL文本文件下载到csv文件python
- c++ - c++中类定义前面的宏
- javascript - 如何在视图中将 js 中的 var 与 Rails 上的 ruby 代码混合
- reactjs - React JS如何更新数组中的值
- c# - 并行运行多个测试时出现 DI / Unity OutOfMemoryException
- python - 熊猫图 - 两个轴标签重叠