So I've read several questions on this but most are in Objective-C and I haven't found any that address/answer this directly. I am new to programming here so please explain any suggestions really thoroughly.
I need to understand how to deallocate my GameScene after game over is reached. The reason I need to do this is because after game over, I transition to a new SKScene (the game over scene) and when I transition back to my GameScene, either GameScene is not reset entirely or I'm generating a new GameScene each time.
I say this because My frames per second count goes down every time I get game over and transition back to the GameScene, so that if I do it enough times I can't even play the game anymore. This is a problem.
I don't understand why this happens because when I transition to my game over scene, I remove everything from my GameScene:
self.viewController!.performSegueWithIdentifier("goToGameOver", sender: nil)
self.removeAllChildren()
self.removeAllActions()
self.scene?.removeFromParent()
I also read here: After Closing SKScene, Memory Remains High that removing the SKView that contains an SKScene deallocates the SKScene, so when both my GameScene and game over scenes are left, I remove BOTH views from their superviews like this:
override func viewDidDisappear(animated: Bool) {
print("its gone")
self.view.removeFromSuperview()
}
But nothing has any affect on the decreasing frames per second count and the game keeps lagging. I don't understand what I'm not doing and can't tell whether the game over scene is the problem or I'm just not deallocating GameScene.
How do I deallocate the GameScene? What else should I be doing?
EDIT:
My node count and draws count remain about the same every time I transition back to GameScene, so that doesn't seem to be a problem. I tried testing whether deinit
was called when transitioning between scenes (I didn't have a deinit
before) using this within the GameScene class:
} //<**End of GAMESCENE**
deinit {
print("Deinit was called")
}
And nothing is printed so deinit
is not being called.
EDIT 2:
@alex_p Here is what I have in my GameViewController-
class GameViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
var scene1: GameScene?
var scene2: GameScene?
var skView: SKView?
// Action to present next Scene
@IBAction func nextSceneAction(sender: AnyObject) {
print("Next scene")
// Create new GameScene object
scene2 = GameScene(fileNamed:"GameScene")
// Present scene2 object that replace the scene1 object
skView!.presentScene(scene2) //ERROR ON THIS LINE
scene1 = nil
}
And I call this method from my GameScene like this:
self.viewController.nextSceneAction(self)
I copied yours exactly but I am receiving the error unexpectedly found nil while unwrapping an Optional value
on the line commented on above. Deinit in not being called.