Ios 在应用程序进入后台时中断SceneKit场景
当我的游戏进入背景模式时,我想完全暂停游戏。目前我所做的就是: 在AppDelegate中:Ios 在应用程序进入后台时中断SceneKit场景,ios,scenekit,Ios,Scenekit,当我的游戏进入背景模式时,我想完全暂停游戏。目前我所做的就是: 在AppDelegate中: func applicationWillResignActive(application: UIApplication) { gameViewControllerDelegate?.pauseGame() } 在我的游戏控制器中: func pauseGame() { buttonPausePressed(buttonPausePlay) } func buttonPausePres
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)
}
func comesFromBackground()
{
let skView: SKView = self.view as! SKView
let scene = skView.scene as! GameScene
scene.comesFromBackground = true
}
调用该方法并更改按钮的图像。甚至游戏也暂停了。但当我再次打开应用程序并使用以下命令取消暂停时:
scnView?.scene?.paused = false
所有的图形变化和其他奇怪的事情都会发生。看起来这些活动从未暂停过。有什么想法吗?我也有同样的问题。在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 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)
}
func comesFromBackground()
{
let skView: SKView = self.view as! SKView
let scene = skView.scene as! GameScene
scene.comesFromBackground = true
}
最后,在游戏场景中:
override func update(currentTime: CFTimeInterval)
{
if comesFromBackground{
comesFromBackground = false
gameViewController.pauseScene()
}
}
变量comesFromBackground是布尔值。
目的是当你再次打开应用程序时,你在游戏场景中设置布尔值,然后再次停止游戏
我希望能在这方面帮助你