简体   繁体   English

展开可选值时,从SKScene过渡到viewController发现为零

[英]Transitioning to viewController from SKScene found nil when unwrapping optional value

so i found some posts about transitioning from Skscene to uiviewcontroller and I got it to work. 所以我找到了一些有关从Skscene过渡到uiviewcontroller的帖子,并且我可以使用它。 This segue and unwind segue is called everytime the user win the level of my game or loses. 每当用户赢得我的游戏级别或输掉游戏时,就会调用这种segue和unsewing segue。

this works for level1 but as soon as I win level2 I get 这适用于Level1,但是一旦我赢得Level2,我就会得到

 fatal error: unexpectedly found nil while unwrapping an Optional value

on the line where I call the game over function below 在下面我称之为游戏结束功能的那一行

in my game scene I have : 在我的游戏场景中,我有:

class GameScene: SKScene, SKPhysicsContactDelegate {

//next level / try again segue
var viewController : GameViewController!

in the GameViewController i initialize this property 在GameViewController中,我初始化此属性

    var currentLevel: Int!


override func viewDidLoad() {
    super.viewDidLoad()
    currentLevel = gameScene.currentLevel
    if let scene = GameScene.level(currentLevel) {
        // Configure the view.
        let skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

        //initialize VC
        scene.viewController = self
    }

 func gameOver() {
    performSegueWithIdentifier("gameOver", sender: nil)
}

@IBAction func perpareForUnwind(unwindSegue: UIStoryboardSegue) {

}

and finally i call gameOver from my win() function in gameScene 最后我从gameScene中的win()函数调用gameOver

    func newGame() {
    view!.presentScene(GameScene.level(currentLevel))
}

func win() {
    if (currentLevel < 3) {
        currentLevel++
        //present win view - with option to restart, next level, or home
    }
    println(currentLevel)
    runAction(SKAction.sequence([SKAction.waitForDuration(2),
        SKAction.runBlock(newGame)]))
    self.viewController.gameOver() // this is the error!
}

So this works from level1 to level2 but wont work from level2 to level3 所以这可以从级别1到级别2工作,但不能从级别2到级别3工作

Since viewDidLoad is only called once it is only initialized from lvl1 to lvl2 and then becomes nil. 由于viewDidLoad仅被调用一次,因此仅从lvl1初始化为lvl2,然后变为nil。 How can i make sure that it is initialized everytime. 我如何确保它每次都初始化。 Shoud I put this set up code somewhere other than viewDidLoad? 我应该把这个设置代码放到viewDidLoad以外的地方吗?

From level 1 to level 2 your var viewController is not nil, because you initialize it to self on viewDidLoad. 从级别1到级别2,您的var viewController不是nil,因为您在viewDidLoad上将其初始化为self。

But then, on your next level, viewController is nil, because your code 但是,在下一个级别,viewController为零,因为您的代码

//initialize VC
scene.viewController = self

only gets executed on viewDidLoad in your GameViewController. 仅在您的GameViewController中的viewDidLoad上执行。

You should set the value of your var viewController in your scene init method or didMoveToView, so it gets set for every new scene. 您应该在场景初始化方法或didMoveToView中设置var viewController的值,以便为每个新场景进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 展开Optional值时意外发现nil - Unexpectedly found nil when unwrapping an Optional value ViewController的实例给出错误“解开可选值时发现nil” - Instance of a ViewController gives error “found nil while unwrapping an Optional value” 在Base ViewController中解开Optional值时意外发现nil - unexpectedly found nil while unwrapping an Optional value in Base ViewController 打开ViewController时,在解开Optional值时意外发现nil - opening ViewController raise unexpectedly found nil while unwrapping an Optional value 发现 nil 时崩溃,同时隐式展开非 nil 的可选值 - Crash when found nil while implicitly unwrapping an Optional value that is not nil Swift Optionals-解开可选值时意外发现nil - Swift Optionals - Unexpectedly found nil when unwrapping an optional value 加载tableView时解开可选值时意外发现nil - Unexpectedly found nil while unwrapping an optional value when loading tableView 解开核心数据中的可选值时发现nil - Found nil when unwrapping optional value in core data 展开可选值时,UiCollectionView单元意外发现为零 - UiCollectionView Cell unexpectedly found nil when unwrapping optional value 添加UINavigation时,“在展开可选值时意外发现nil” - “Unexpectedly found nil while unwrapping an Optional value” When UINavigation is added
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM