当我的游戏进入后台模式时,我想完全暂停游戏。目前这就是我所做的:
在 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/