错误"线程1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)"使用Swift SpriteKit

时间:2015-12-18 07:34:44

标签: ios xcode swift crash uibutton

我正在尝试在GameViewController中运行GameScene函数,并且收到上面提到的错误。以下是代码的基础知识:

这在GameScene中调用:

func resetPosition(){
     someNode!.position = otherPosition! //this is the line with the error - otherPosition IS called previously as a CGPoint
}

在GameViewController中:

let gameScene = GameScene.self()

func reset(){ // reset is called by a UIButton
     gameScene.resetPosition()
}

模拟器打开并运行正常,但是一旦按下链接到重置的按钮,应用程序就会崩溃。如果您需要更多代码,我很乐意提供。

1 个答案:

答案 0 :(得分:0)

听起来好像someNodeotherPosition都是零。你说早先调用了otherPosition,所以它必须是someNode

如果您在更新位置之前尝试解包someNode会怎样?

if let someNode = someNode {
   someNode.position = otherPosition
} else {
    print("someNode was nil")
}