Swift, spritekit: How to restart GameScene after the game? Stop the lag?

Ok, so I have a sprite kit in Swift, and I'm having trouble reloading GameScene after the game.

Right now, when the user loses his whole life, the gameIsOver variable gameIsOver set to true , which pauses certain nodes within the scene and also turns off the timer. At the end of this timer, I proceed to the scene with the game. From the Game Over scene, the user can either return to their homeland or restart the game.

Here's how I get to my stage game:

  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 because if I don’t, when GameScene restarts, gameIsOver is still set to true and my application crashes. I do not know why this happens if I set gameIsOver to false here.

After moving from GameScene to my game on the stage and back to GameScene or from GameScene to my home view controller and back to GameScene a couple of times, my fps score has decreased so much that the whole game lags behind the point where the gameplay is impossible.

This makes me think that I am not deleting / disposing of GameScene properly every time I present my game on top of the stage.
I believe that I have the same problem as here: In Swift on "game over", move from scene to another UIView and remove the scene? but I am new to this and I cannot understand how they solved their problem.

How can I completely reset / delete GameScene every time I submit my Game Over scene to stop the lag?

+5
source share
1 answer

Your gameIsOver Bool will not retain the value when switching between scenes, unless you make it a structure. Therefore, instead of

 var gameIsOver = false 

he should be

 struct game { static var IsOver : Bool = false } 

therefore, when you change the value, as everything happens, 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 

Regarding the transition back to 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 in GameScene just call

 goToGameScene() 
+5
source

Source: https://habr.com/ru/post/1234436/


All Articles