Chris Hutchison Chris Hutchison - 1 year ago 68
iOS Question

Does a scene have to be removed when changing scenes?

In sprite-kit I'm making a game that includes various levels in different scenes. Im curious if just transitioning to a scene will remove the other scene or will they continue to pile up and take up cpu usage? I'm transitioning them all in this sort of fashion.

let reveal = SKTransition.fade(with: UIColor.white, duration: 1.5)
let scene = level2(size: self.size)
self.view?.presentScene(scene, transition:reveal)

Answer Source

Good practice #1

It's usually a good practice keeping only one scene in memory while your are executing your game.

Good practice #2

Another good practice is avoiding strong retaining reference loops. Specifically should not exist a descendant node of your scene with a strong reference to the scene itself.


That said when you invoke

 self.view?.presentScene(scene, transition:reveal)

the strong reference from the SKView to the scene is removed. So, if you respected the 2 assumptions above, the scene will automatically be deinitialized and removed from memory.


You can test it yourself. Just add this to your game scene

deinit {
    print("GameScene deinit")

This method is automatically invoked when the instance is deinitialized.

Now run

 self.view?.presentScene(scene, transition:reveal) 

and look for the "GameScene deinit" into the log.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download