pausing spritekit game using appdelegate in ios8

2020-07-31 03:27发布

问题:

I have a pause menu in my game that I want to toggle when the i hit my home button. it works when I'm within my game, however when I hit my home button the menu comes up, but the game will not pause.

when i restart the app, the menu is still there, but the game un-pauses itself. It seems like spritekit automatically pauses and restarts the game on its own when you leave and enter the app. When I'm within the app I have full control over it. But when I'm exiting/entering the app it behaves how it likes.

Any suggestions?

func applicationWillResignActive(application: UIApplication!) {
    // get the root viewcontroller
    // toggle my pausemenu method.  pause menu sets skview.paused = true
}

回答1:

Send an NSNotification to GameSceneViewController to pause the SKScene:

AppDelegate:

- (void)applicationWillResignActive:(UIApplication *)application
{    
    // Pause sprite kit scene
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PauseGameScene" object:self];
}

GameSceneViewController:

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(pauseGameScene)
                                                 name:@"PauseGameScene"
                                               object:nil];
}

- (void)pauseGameScene {
    if (self.skView.scene) {
        self.skView.paused = self.skView.scene.paused = YES;
    }
}