游戏在 swift 从后台恢复后从暂停状态退出 [英] game exits from pause state after resuming it from background in swift

查看:23
本文介绍了游戏在 swift 从后台恢复后从暂停状态退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SpriteKit 开发一款游戏,该游戏可以在执行期间暂停并可以恢复.但是当我在游戏暂停时按下主页按钮时 applicationDidEnterBackground 有问题,因为当我恢复游戏时,即使游戏之前暂停,实体也会立即开始移动.我找不到在 AppDelegate 中实现 applicationDidEnterBackground 和其他相关方法的方法,因为它和我的 GameScene.swift

I'm developing a game with SpriteKit that can be paused during execution and can be resumed. But I have a problem with applicationDidEnterBackground when the home button is pressed while the game is paused because when I resume the game the entities start moving immediately even though the game was paused before. I cannot find a way to implement applicationDidEnterBackground and other related method in AppDelegate since there is no connection between that and my GameScene.swift

我实际上是在用代码暂停实体

I'm actually pausing the entities with the code

entitiesLayerNode.paused = true
gameState = .GamePaused

我想显式地暂停 entitiesLayerNode,因为我还有其他要保留的移动"节点.问题是,根据下面给出的建议,该方法会暂停一切!我只需要暂停那一层.我认为这个问题是我无法从视图控制器访问 entitiesLayerNode.我通常使用代码段

I want to explicitly pause the entitiesLayerNode because I have other 'moving' nodes that I want to keep. To the problem is that with the suggestion given below that method pauses everything! I just need to pause that layer. I think that problem is that I cannot access the entitiesLayerNode from the View Controller. I generally use the snippet

let mainScene = scene as GameScene
mainScene.entitiesLayerNode.pause = true

但是 Xcode 给了我一个错误,即 scene 是一个未解析的标识符.

But Xcode gives me an error that scene is an unresolved identifier.

推荐答案

这里

应用程序委托:

func applicationWillResignActive(application: UIApplication) {
    NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self)
}

GameSceneViewController:

GameSceneViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil)
}

func pauseGameScene() {
    if self.skView.scene != nil {
        self.skView.paused = self.skView.scene.paused = true
    }
}

这篇关于游戏在 swift 从后台恢复后从暂停状态退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆