0

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!:)

GeekSince1982
  • 732
  • 10
  • 25
  • you only use perform with segue, the old viewController will not get released. You need to pop back or reset window.rootViewController to break retain cycle. – SolaWing May 24 '15 at 11:50
  • @SolaWing pop back - you mean go back to where? to the view controller which loads the scene? and if i want to reset - how to do this properly? – GeekSince1982 May 24 '15 at 11:59
  • Yes. you can go back through `dismissViewControllerAnimated` or `popViewControllerAnimated`, Depend on if you use a NavigationController. If you want to reset, depend on which property hold the original home ViewController(the `window.rootViewController` if it's your initial viewController, or NavigationController's viewControllers property), you can just overwrite it with new home ViewController. – SolaWing May 24 '15 at 12:13
  • @SolaWing I think I'm doing something wrong. So I prefer to reset the whole thing. As per code above - I perform segue - so the Game Over VC pops up. How can I at that moment "drop" the gameviewcontroller with a scene in it, to be recreated again when i move to home screen and start the game again? I'm a little lost to where and how to overwrite the rootvc... Sorry:( – GeekSince1982 May 24 '15 at 12:33
  • Why are you segueing to a new view controller? Why not just present a new `scene` using the `presentScene` method on `SKView`. Have a look at [this](http://stackoverflow.com/questions/24111935/how-to-transition-scenes-in-swift) – ABakerSmith May 24 '15 at 12:49
  • you can get window from app delegate. or `keyWindow` property of sharedApplication. @ABakerSmith's solution may better. since I am not familiar with SpriteKit. – SolaWing May 24 '15 at 13:02

1 Answers1

2

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.

Wraithseeker
  • 1,884
  • 2
  • 19
  • 34
  • Yes... I think I will do that, but how to then go back to home screen vc from which game scene can be started again andv the game overv scene can be released? – GeekSince1982 May 24 '15 at 14:07
  • To go back to home screen, you simply present the SKScene Home screen class again. The old game over scene will be released – Wraithseeker May 24 '15 at 14:36
  • Ok, got it. I will change my code to use game over SKScene which presents all game over info and then switches to home screen which then if game started, then game SKScene presented instead of game over. Thanks! Will try that! – GeekSince1982 May 24 '15 at 22:12