jeudi 26 mars 2015

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. This segue and unwind segue is called everytime the user win the level of my game or loses.


this works for level1 but as soon as I win level2 I get



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



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



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




Aucun commentaire:

Enregistrer un commentaire