-->

game exits from pause state after resuming it from

2020-03-07 06:40发布

问题:

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