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

查看:137
本文介绍了游戏从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 因为我有我希望保留的其他移动节点。问题在于,根据下面给出的建议,该方法会暂停一切!我只需要暂停那一层。
我认为问题是我无法从View Controller访问 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给我一个错误,场景是一个未解析的标识符。

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

推荐答案

here

AppDelegate:

AppDelegate:

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天全站免登陆