ios - 在应用程序进入后台时中断 SceneKit 场景

标签 ios scenekit

当我的游戏进入后台模式时,我想完全暂停游戏。目前这就是我所做的:

在 AppDelegate 中:

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

在我的游戏 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)
}

该方法被调用并且按钮的图像被更改。连比赛都暂停了。但是当我再次打开应用程序并使用以下命令取消暂停时:

scnView?.scene?.paused = false

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

最佳答案

我遇到了同样的问题。在你的 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 是 bool 值。 目的是当您再次打开应用程序时,您在 gameScene 中设置 bool 值,然后再次停止游戏。

希望能帮到你

关于ios - 在应用程序进入后台时中断 SceneKit 场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36164557/

相关文章:

ios - 具有半透明图层内容的 SCNMaterial 显示为白色

swift - iOS 14 上的 Arkit/SceneKit 引发新警告( Metal )

ios - iPad Pro 的平台字符串

ios - 在横向模式下在 iPad 上呈现 UIImageViewController 时崩溃

objective-c - 在 iOS 中验证 Dropbox

ios - SCNMaterial 内容中的 SKScene 出现颠倒

ios - 如何将在 SceneKit 中创建的对象从一个点移动到另一个点

swift - SceneKit 中的低帧率

iphone - UIImage 第一次加载时卡顿

ios - 如何隐藏 Google map 位置点