5

Alright, so I have a sprite kit game in Swift here and I'm having trouble restarting my GameScene after it's game over.

Right now, when the user loses all their lives, a variable gameIsOver is set to true, which pauses specific nodes within the scene as well as sets off a timer. After this timer ends, I move to my Game Over scene. From the Game Over scene, the user can either go back to home or restart the game.

Here is how I transition to my game over scene:

    countdown(circle, steps: 120, duration: 5) { 

                //Performed when timer ends
                self.gameSoundTrack.stop()

                let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = mainStoryboard.instantiateViewControllerWithIdentifier("GOViewController")
                self.viewController!.presentViewController(vc, animated: true, completion: nil)

                //Resetting GameScene
                self.removeAllChildren()
                self.removeAllActions()
                self.scene?.removeFromParent()
            self.paused = true
            gameIsOver = false
        }

I pause my scene here is because, if I don't, when GameScene is reloaded gameIsOver is still set to true and my app crashes. I don't know why this occurs considering I set gameIsOver to false here.

After moving from GameScene to my game over scene and back to GameScene, or from GameScene to my home view controller and back to GameScene a couple times, my fps count has decreased so much that the whole game is lagging to the point where gameplay is impossible.

This leads me to believe that I am not removing/disposing of GameScene properly each time I present my game over scene.
I believe I'm having the same problem as here: In Swift on "game over" move from scene to another UIView and dispose the scene? , but I'm new to this and I can't figure out how they solved their problem.

How I can I completely reset/delete GameScene each time I present my Game Over scene in order to stop the lagging?

Community
  • 1
  • 1
blue
  • 7,175
  • 16
  • 81
  • 179

1 Answers1

5

Your gameIsOver Bool will not keep a value while switching between scenes unless your make it a struct. So instead of

var gameIsOver = false

it should be

struct game {
    static var IsOver : Bool = false
}

so when you change the value as things happen you call

game.IsOver = true
//if your calling it in a different View controller or scene than where you created it just put the name before it like so
GameViewController.game.IsOver = true

As for the transition back to your GameScene create a function

func goToGameScene(){
    let gameScene:GameScene = GameScene(size: self.view!.bounds.size) // create your new scene
    let transition = SKTransition.fadeWithDuration(1.0) // create type of transition (you can check in documentation for more transtions)
    gameScene.scaleMode = SKSceneScaleMode.Fill
    self.view!.presentScene(gameScene, transition: transition)
    }

then whenever you want to reset to GameScene just call

goToGameScene()
Timmy Sorensen
  • 570
  • 6
  • 16
  • 1
    oh, so there's nothing wrong on doing that? re-calling the same scene over and over again? wow. I thought it would be kinda "heavy" for the game engine. – msqar Jul 21 '16 at 17:16
  • 2
    No it will create a new scene and it does not hold on to the old scene. If you were using a navigation controller and using segues it would be different. Be sure to mark the answer as correct if it helped you out. – Timmy Sorensen Jul 21 '16 at 19:53
  • im not the post owner :( unfortunately i can't give you the mark. But you helped me with this so im the one who gave you the up vote. – msqar Jul 22 '16 at 17:37
  • Just noticed the difference between let gameScene = GameScene:size and GameScene:fileNamed -- namely GameScene:size set's the scene's anchorPoint to (0, 0), where :fileNamed sets anchorPoint to (0.5, 0.5). The first time I tried the above, my new scene started in the lower-left corner. I had to add gameScene.anchorPoint = CGPoint(x: 0.5, y: 0.5) to get my new scene back to the center of the screen. See discussion here: https://stackoverflow.com/questions/41649446/swift-3-spritekit-reseting-the-gamescene-after-the-game-ends – Eric Feb 13 '18 at 05:23