so now I am programming a game and I am experiencing a problem on which I really have not found a solution. I will try to describe it for you.
So, to better explain my problem, I created a schematic drawing for you guys:
Now here is the problem: I have two View Controllers, one called MenuVc
, the other - GameVC
. In GameVC
I have SKView
, which shows the specific SKScene
when called didMoveToView
, called GameScene
. GameScene
has a method didBeginContact
that then moves on to another SKScene
that SKView
has, called GameOverScene
, with the following code (Segue 1 in the figure):
let reveal = SKTransition.crossFadeWithDuration(0.5)
let gameOverScene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(gameOverScene, transition: reveal)
Here the user can return to GameScene
to restart the game (Segue 2 in the figure). There GameScene
is another button that will allow you to return to MenuVc
through the event NSNotificationCenter
(Segue 3 in the drawing). The code for this is as follows:
In GameViewController.swift:
override func awakeFromNib() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "goToMenuViewController:", name: "GoToMenuViewController", object: nil)
}
@objc func goToMenuViewController(notification: NSNotification){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MenuVC")
vc.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(vc, animated: true, completion: nil)
}
deinit{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
In GameScene.swift, when you click the BackButton button:
NSNotificationCenter.defaultCenter().postNotificationName("GoToMenuViewController", object: self)
So, if now I want to return to GameVC
using the button on MenuVc
(Segue 4 in the figure), everything works fine. SKView
in GameVC
again shows GameScene
, but when now called didBeginContact()
, it does not represent GameOverScene
. The function is called, I checked it, and it also executes the code in it, but does not execute segue (1), as you would expect. Do you have ideas why and what should I change? Is this something to deactivate when Segue 3 is called?
Code for GameVC
viewDidLoad
:
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
Segue 4:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("GameVC")
vc.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(vc, animated: true, completion: nil)