GeekSince1982
GeekSince1982

Reputation: 731

In Swift on "game over" move from scene to another UIView and dispose the scene?

I have a "life counter" in my game and when it hits 0 = dead - i want to move away from the scene to another UIView - which is game over view - with stats and a button to go back to the home screen (first UIViewController with buttons to start the game and so on.

Here is the code of how i move to the Game Over view

class GameScene: SKScene,SKPhysicsContactDelegate {
var viewController: UIViewController?
// more code and functions
// ......

 func trackLife (lifeCHange: Int){
    life = life + lifeCHange
    lifeLabel.text = String(life)
    if life < 1 {
        // Go to Game Over VC
        self.removeAllChildren()
        self.removeAllActions()
        self.scene?.removeFromParent()
        self.viewController!.performSegueWithIdentifier("gameOverSegue", sender: viewController)

    }
  }
}

this works for presenting the game over view, but what i thinks i'm not "disposing" or reseting the scene. because if i do this in a loop:

Start Game --> Game Over --> Back to The Home Screen --> Start Game --> Game Over....

I see the memory usage grows on each cycle:) I guess i'm just adding scenes but not removing them?

I'm sorry - i'm fresh to this. Will be very grateful for your experience!:)

Upvotes: 0

Views: 2189

Answers (1)

Wraithseeker
Wraithseeker

Reputation: 1904

To manage your memory effectively in Sprite-Kit, you should create another SKScene for your GameOver screen to be presented from your main screen. In this way, the old SKScene would be released.

Upvotes: 2

Related Questions