我正在用 swift 制作一个游戏,我得到了 “ fatal error :在解包可选值时意外发现 nil” 在 shotMarsh 方法中。我想要做的就是将nodeNameA?.node?.position 传递给shootMarsh 方法以使用节点的位置。由于我是 swift 和 spritekit 的新手,一些提示和示例将非常有帮助!
if ((nodeNameA.categoryBitMask == category_marsh && nodeNameB.categoryBitMask == PhysicsCategory.Bullet) ||
(nodeNameA.categoryBitMask == PhysicsCategory.Bullet && nodeNameB.categoryBitMask == category_marsh)) {
var pos = nodeNameA?.node?.position
shootMarsh(pos!) //Getting the error here
if contact.bodyA.node != playerSprite {
nodeNameA.node?.removeFromParent()
最佳答案
您可以使用 if let
以这种方式解包可选值:
if let pos = nodeNameA?.node?.position{
shootMarsh(pos)
}
现在当您的 pos
实例变为 nil
时它不会崩溃。
关于ios - swift spritekit中解析Nodes位置时出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32665811/