Why doesn't my ViewController present another SKScene after reopening it?

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:

Schematic drawing

Now here is the problem: I have two View Controllers, one called MenuVc, the other - GameVC. In GameVCI have SKView, which shows the specific SKScenewhen called didMoveToView, called GameScene. GameScenehas a method didBeginContactthat then moves on to another SKScenethat SKViewhas, 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 GameSceneto restart the game (Segue 2 in the figure). There GameSceneis another button that will allow you to return to MenuVcthrough 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 GameVCusing the button on MenuVc(Segue 4 in the figure), everything works fine. SKViewin GameVCagain 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)
+4
1

, , .

, UIK , .

+2

Source: https://habr.com/ru/post/1613238/


All Articles