与按钮交互时意外地找到nil?

时间:2018-11-08 00:52:59

标签: ios swift sprite-kit

我正在为SpriteKit游戏编写“游戏结束”场景。由于某些原因,当我单击“ replayButton”时,游戏将崩溃,但如果单击“ returnButton”,它将按预期运行。两个按钮的代码相同。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first

    if let location = touch?.location(in: self) {
        let tracker = self.nodes(at: location)

        if tracker.first?.name == "replayButton" {
            let change = SKTransition.flipVertical(withDuration: 1)
            let gamePlayScene = SKScene(fileNamed: "GamePlayScene")
            gamePlayScene?.scaleMode = .aspectFill
            self.view?.presentScene(gamePlayScene!, transition: change)


        }
        else if tracker.first?.name == "returnButton" {
            let change = SKTransition.flipVertical(withDuration: 1)
            let nextScene = SKScene(fileNamed: "MenuScene")
            nextScene?.scaleMode = .aspectFill
            self.view?.presentScene(nextScene!, transition: change)
        }
    }
}

错误是“线程1:致命错误:在打开if语句的第一行时意外发现nil”

self.view?.presentScene(gamePlayScene!, transition: change)

我的困惑是一个按钮有效,但另一个按钮却用相同的代码给我错误?

更新:它可以正常工作-之所以不能正常工作,是因为我缺少SKS以及GamePlayScene的swift文件!

0 个答案:

没有答案