game exits from pause state after resuming it from background in swift

一个人想着一个人 提交于 2019-12-30 07:29:10

问题


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

EDIT:

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

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


回答1:


Pretty much the same as here

AppDelegate:

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

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
    }
}


来源:https://stackoverflow.com/questions/27655162/game-exits-from-pause-state-after-resuming-it-from-background-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!