【发布时间】:2017-03-17 03:29:03
【问题描述】:
在我被反对票钉在十字架上之前,让我说我已经对此进行了研究,但我仍然不明白为什么会出现这个错误。
我有一个玩家试图防御的核心,你可以从它发射小激光来防御来袭的流星。好吧,我已经完成了所有设置和工作(大部分时间),但是当激光击中流星并且我的碰撞处理功能试图移除射击节点和流星节点时,它会抛出这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我再次对此进行了大量研究,但我似乎无法弄清楚。
这是我的 didBegin:
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.node?.name == "shot") { // shot A
collisionBetween(first: (contact.bodyA.node)!, second: (contact.bodyB.node)!)
} else if (contact.bodyB.node?.name == "shot") { // shot B
collisionBetween(first: (contact.bodyB.node)!, second: (contact.bodyA.node)!)
}
if (contact.bodyA.node?.name == "core") { // core A
collisionBetween(first: (contact.bodyA.node)!, second: (contact.bodyB.node)!)
} else if (contact.bodyB.node?.name == "core") { // core B
collisionBetween(first: (contact.bodyB.node)!, second: (contact.bodyB.node)!)
}
}
这是我的碰撞函数:
func collisionBetween(first: SKNode, second: SKNode) {
if first.name == "shot" && second.name == "sMeteor" {
first.removeFromParent() // these two only throw the error
second.removeFromParent() // every once and a while
} else if first.name == "sMeteor" && second.name == "shot" {
first.removeFromParent() // these two throw the error also
second.removeFromParent() // but only on occasion
} else if first.name == "mMeteor" && second.name == "shot" {
second.removeFromParent()
} else if first.name == "shot" && second.name == "mMeteor" {
first.removeFromParent()
}
if first.name == "core" && second.name == "sMeteor" {
second.removeFromParent() //always throws the error
} else if first.name == "sMeteor" && second.name == "core" {
first.removeFromParent() //always throws the error
}
}
我一直在尝试解决这个错误一段时间,我觉得我对选项有了基本的了解。仅当我尝试从父 self 中删除节点时才会引发这些错误,并且我尝试了许多不同的方法来解决此问题,但我终生无法弄清楚。任何帮助表示赞赏,谢谢!
【问题讨论】:
标签: swift sprite-kit collision-detection physics optional