SceneKit scene interruption when entering the application

When my game enters the background, I would like to completely pause the game. At the moment, this is what I am doing:

In AppDelegate:

func applicationWillResignActive(application: UIApplication) {
    gameViewControllerDelegate?.pauseGame()
}

In my game controller:

func pauseGame() {
    buttonPausePressed(buttonPausePlay)
}

func buttonPausePressed(sender: UIButton!) {
    scnView?.scene?.paused = true
    stopMusic()
    let exampleImage = UIImage(named: "16-play")?.imageWithRenderingMode(.AlwaysTemplate)
    sender.setImage(exampleImage, forState: UIControlState.Normal)
}

The method is called and the button image changes. Even the game is paused. But when I open the application again and do not stop it using:

scnView?.scene?.paused = false

all graphics changes and other strange things happen. It looks like SCNActions never stopped. Any ideas?

+4
source share
1 answer

I had the same problem. In AppDelegate.swift

func applicationWillResignActive(application: UIApplication) 
{
    let viewControllers = self.window?.rootViewController?.childViewControllers
        for vc in viewControllers!
        {
            if vc.isKindOfClass(GameViewController)
            {
                let view = vc as! GameViewController
                view.comesFromBackground()
            }
        }
   }

And then, in your GameViewController:

func comesFromBackground()
{
    let skView: SKView = self.view as! SKView
    let scene = skView.scene as! GameScene
    scene.comesFromBackground = true
}

Finally, in your GameScene:

override func update(currentTime: CFTimeInterval)
{
    if comesFromBackground{
        comesFromBackground = false
        gameViewController.pauseScene()
    }
}

FromBackground . , , Scene .

+3

All Articles