进入后台时中断SceneKit场景

5
当我的游戏进入后台模式时,我希望完全暂停游戏。目前我的做法如下所示:
在AppDelegate中:
func applicationWillResignActive(application: UIApplication) {
    gameViewControllerDelegate?.pauseGame()
}

在我的游戏控制器中:
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)
}

该方法被调用后,按钮的图像会更改,即使游戏已暂停。但是当我再次打开应用程序并使用以下方法取消暂停时:

scnView?.scene?.paused = false

所有的图形变化和其他奇怪的事情发生了。似乎SCNActions从未被暂停过。有什么想法吗?

1个回答

4

我有同样的问题。在你的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()
            }
        }
   }

然后,在您的GameViewController中:

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

最后,在您的GameScene中:

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

变量comesFromBackground是布尔类型。其目的是当您再次打开应用程序时,在游戏场景中设置布尔值,然后再次停止游戏。
希望这能帮助您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接