Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift, spritekit: How to restart GameScene after game over? Stop lagging?

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?

like image 316
blue Avatar asked Oct 24 '15 17:10

blue


1 Answers

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()
like image 130
Timmy Sorensen Avatar answered Nov 07 '22 11:11

Timmy Sorensen