I am making a game in SpriteKit using Swift that currently has two viewControllers. The first viewController is the GameViewController that displays the menu with some buttons positioned in interface builder using storyboards. There is a play button on the menu that transitions to the second viewController that displays the gameScene. Both viewControllers load an SKScene.
To transition from one view controller to the other and back again i am using segues created in storyboard.
The problem i am having is that the memory increases when ever i switch between the viewControllers causing the app to eventually crash. I have a feeling the viewControllers are just stacking on each other. Here is my code for GameViewController and GameSceneViewController.
GameViewController:
class GameViewController: UIViewController, GKGameCenterControllerDelegate {
//MARK: Variables
var scene: Menu!
@IBOutlet weak var settingsBtn: UIButton!
@IBOutlet weak var leaderboardsBtn: UIButton!
@IBOutlet weak var storeBtn: UIButton!
@IBOutlet weak var playBtn: UIButton!
@IBAction func play(sender: UIButton) {
}
//MARK: Button Animations
func menuButtonAnimations(){
settingsBtn.transform = CGAffineTransformMakeTranslation(0, 200)
leaderboardsBtn.transform = CGAffineTransformMakeTranslation(0, 200)
storeBtn.transform = CGAffineTransformMakeTranslation(0, 200)
springWithDelay(0.5, 0.7, {
self.settingsBtn.transform = CGAffineTransformMakeTranslation(0, 0)
})
springWithDelay(0.5, 0.75, {
self.leaderboardsBtn.transform = CGAffineTransformMakeTranslation(0, 0)
})
springWithDelay(0.5, 0.8, {
self.storeBtn.transform = CGAffineTransformMakeTranslation(0, 0)
})
}
//MARK: The View
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = false
skView.showsNodeCount = false
skView.showsPhysics = false
skView.ignoresSiblingOrder = true
scene = Menu(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene)
//Button Animation
menuButtonAnimations()
}
//MARK: Extra Stuff
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
GameSceneViewController:
class GameSceneViewController: UIViewController{
//MARK: Variables
@IBOutlet weak var returnBtn: UIButton!
//MARK: Helper Functions
//MARK: The View
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Configure the view.
var scene: GameScene!
let skView = self.view as SKView
skView.showsFPS = false
skView.showsNodeCount = false
skView.showsPhysics = false
skView.ignoresSiblingOrder = true
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene)
//Notifications
}
The segues are both modal segues created using the linking in storyboard.
Any help on how to transition back and forth between the two viewControllers without constantly increasing the memory would be great!