SpriteKit- the right way to multitask

后端 未结 6 1292
梦毁少年i
梦毁少年i 2020-12-13 01:11

I tried to search everywhere in the code:Explained documentry what it does when going to background, or if it is even paused sometime, but to no avail- can someone direct me

6条回答
  •  囚心锁ツ
    2020-12-13 01:36

    For me did not work none of one provided solutions. I did found another way how to do it.

    // In the AppDelegate
    - (void)applicationWillResignActive:(UIApplication *)application
    {
        if ([self.window.rootViewController isKindOfClass:[GameViewController class]]) {
            GameViewController *vc = (GameViewController*)self.window.rootViewController;
            [vc pauseGame];
            [vc.view addObserver:vc
                      forKeyPath:@"paused"
                         options:NSKeyValueObservingOptionNew
                         context:nil];
        }
    }
    
    // In the GameViewController
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        SKView *skView = (SKView*)self.view;
        MainGame *mainGame = (MainGame*)skView.scene;
        if ([keyPath isEqualToString:@"paused"] &&
            [[change objectForKey:NSKeyValueChangeNewKey] boolValue] == NO &&
            [mainGame isGameInProgress]) {
            [self.view removeObserver:self forKeyPath:@"paused" context:nil];
            skView.paused = YES;
        }
    }
    

提交回复
热议问题