Cing
Cing

Reputation: 816

Game restarts after segue to gameOverViewcontroller

I have a problem with my sprite kit game, it restart immediately after i use a model segue to my gameoverviewcontroller.
The fps also gets divide by 2 after each time a restart the game.
So i actually need a way to delete the game after i use the segue.
I have tried self.dismissViewControllerAnimated(true, completion: nil) but i don't really know where i should put.

I have in my storyboard 3 view controllers 1 for the start who uses a button with a modal segue to the second one where i have the game scene played.

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    print("start vc")

    // Configure the view.
    let skView = self.view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.multipleTouchEnabled = true

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

    /* Set the scale mode to scale to fit the window */
    Scene = GameScene(size: skView.bounds.size)
    Scene.scaleMode = .AspectFill
    Scene.viewController = self

    skView.presentScene(Scene)
}

at the end when the player dies i use this in game scene.swift

self.viewController?.performSegueWithIdentifier("GameOver", sender: self.viewController!)

and this in gameviewcontroller.swift

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "GameOver" {
        let destViewController: GameOverViewController = segue.destinationViewController as! GameOverViewController
        destViewController.Score = Scene.Score
        destViewController.screenShot = Scene.image
        print("segue")
        Scene.viewController = nil
    }
}

to go to the third view controller.

Now i have put multiple print command throwout my code.
And i saw that the game automatically restarts after segue is completed. Has any body an idee how i could solve this problem or knows how i maybe should use the dismiss function.

Thanks in regards

Upvotes: 4

Views: 79

Answers (1)

giorashc
giorashc

Reputation: 13713

viewWillLayoutSubviews of the current view controller will be called even when changing to another view controller.

So in your case protect the view so the scene won't be created twice (thus, the game restart) :

 override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    print("start vc")

    // Configure the view.
    let skView = self.view as! SKView
    if skView.scene == nil { // This check makes sure we won't create the scene twice
       skView.showsFPS = true
       skView.showsNodeCount = true
       skView.multipleTouchEnabled = true

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

       /* Set the scale mode to scale to fit the window */
       Scene = GameScene(size: skView.bounds.size)
       Scene.scaleMode = .AspectFill
       Scene.viewController = self

       skView.presentScene(Scene)
     }
}

Upvotes: 2

Related Questions