pausing spritekit game using appdelegate in ios8

我的梦境 提交于 2019-12-22 00:36: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;
    }
}


来源:https://stackoverflow.com/questions/26066914/pausing-spritekit-game-using-appdelegate-in-ios8

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