0
votes

I'm working on a spritekit game that uses both ViewControllers and a SpriteKit Scene. Basically, the structure of the application is that you open into a menu (view controller), which through a button press modally segues you to the game (view controller) which loads the GameScene (skscene).

When you lose the game, you are sent to a game over screen (view controller) through a modal segue: gameVC?.performSegueWithIdentifier("goToGameOver", sender: gameVC) This segue is in the GameScene, and scene.gameVC = self is in the Game View Controller. gameVC is initialized in GameScene as var gameVC: UIViewController?. In the game over screen, you can either go back to the menu or you can play again (go back to game view controller) with button-initialized modal segues.

The problem occurs when you go to the GameScene more than four times. The fifth time that GameScene is opened (either from the menu vc or from the game over vc), the game lags incredibly. The fps counter at the bottom right of the screen says that the game produces 1.5 fps.

I believe the problem is that the GameViewController & the GameScene are not being correctly dismissed (the memory usage of the app increases every time the app moves to GameScene), but I don't know how to correctly dismiss them. I have used

self.removeFromParent()
self.view?.presentScene(nil)

as a way of trying to dismiss the GameScene right before and right after the segue to GameOver code runs, but to no avail. I also thought of unwind segue, but I cannot unwind to a view controller (game over) that I haven't been to yet. I am all out of ideas, and this is a gamebreaking bug. I would really appreciate some help. Thanks very much.

2
Probably because nothing ever dies, you need to look into strong retain cycles that may be happening. declare deinit in your class files and put a print statement in them to make sure they get calledKnight0fDragon
Before I segue from the game to game over, I do self.removeAllChildren() and `self.removeAllActions()' - does this answer the strong retain cycles?Comrod33
no, that won't help at all since those should die with the parent, something inside of self is holding onto selfKnight0fDragon
When you say 'self is holding onto self', are you saying that something in the GameViewController still references scene.gameVC even after the transition has been made to GameOverViewController? I also checked for memory leaks with Instruments, and although it said that there weren't any leaks, the memory used increases every time GameScene is loaded.Comrod33
Self could mean anything. Whatever instance is not dying, something inside of it is holding onto itKnight0fDragon

2 Answers

3
votes

You are basically doing what you are not supposed to do in SpriteKit. Using multiple view controllers is not recommended in SpriteKit.

Its better to just have 1 view controller and have multiple scenes (menu scene, game scene).

You should read this question.

Swift: How to handle view controllers for my game

0
votes

Hey I had the same exact problem you had. I put this line of code at the end of my scene to go back to my viewcontroller, and the lag went away.

self.viewController!.dismissViewControllerAnimated(true, completion: nil)