I am trying to segue from a root view controller -> game view controller, then display a skscene of gameplay on gameviewcontroller, then go to a different skscene ( a game over scene), then unwindsegue to the root view controller, and then repeat. The problem is, when I try seguing to the game view controller the second time around, it throws exc_bad_access after the game view controller attempts to present the skscene (gameplay). There is no stacktrace, just an exc_bad_access on the first line of the appdelegate. When I run instruments w/ zombies the app does not throw any error, it works as intended. Putting an exception breakpoint does not seems to do anything. I'm not sure where or what my error is.
this is my root view controller:
class RootViewController: UIViewController {
//view that we present
@IBOutlet weak var MyLabel: UILabel?
var preload = true
override func viewDidLoad() {
super.viewDidLoad()
if preload {
textures.atlas.preloadWithCompletionHandler( {
print("finished preloading")
})
}
}
var lives: Int = 3
@IBOutlet weak var MyStepper: UIStepper!
@IBAction func didChangeValue(sender: UIStepper) {
lives = Int(sender.value)
MyLabel?.text = "lives: \(Int(sender.value).description)"
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
originalLives = lives
enemyLives = lives
heroLives = lives
print("seguing")
}
@IBAction func goBack(unwindSegue: UIStoryboardSegue) {
preload = false
}
}
//Controls a /view/ that displays scenes.
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//initialize the scene to fill screen
let myScene = GameScene(size: self.view.bounds.size)
myScene.scaleMode = .AspectFill
let theskView = self.view as! SKView
self.view.multipleTouchEnabled = true
// theskView.frameInterval = 2
//
// theskView.showsPhysics = true
// theskView.showsFPS = true
// theskView.showsNodeCount = true
// theskView.showsDrawCount = true
theskView.ignoresSiblingOrder = true
myScene.scaleMode = .AspectFill
myScene.size = theskView.bounds.size
myScene.controller = self
theskView.presentScene(myScene)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let dvc = segue.destinationViewController as? RootViewController {
}
}
deinit {
print("game view dies")
}
}
the scenes are straightforward: this is called (from the first skscene) to go to the game over scene
func gameOver(won: String) {
self.removeAllChildren()
let sceneToGoTo = GameOverScene()
sceneToGoTo.won = won == "nudgit" ? true : false
let theskView = self.view! as SKView
theskView.multipleTouchEnabled = true
// theskView.frameInterval = 2
theskView.ignoresSiblingOrder = true
sceneToGoTo.scaleMode = .AspectFill
sceneToGoTo.size = theskView.bounds.size
sceneToGoTo.controller = controller
theskView.presentScene(nil)
theskView.presentScene(sceneToGoTo)
}
and then the gameover scene segues from a touch:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
controller?.performSegueWithIdentifier("segue", sender: self)
}
where "controller?" is a weak var of the game view controller
I have checked deinits: both game scenes are deinitializing correctly, and the gameViewController is correctly deinitializing when it unwinds to the root view controller. I am at a lost as to what I could do to fix this.