SpriteKit - how to correctly pause and resume app

血红的双手。 提交于 2019-12-29 06:56:24

问题


I have huge issue with my newest game for iPhone made with Games by Tutorials book's help.

NOTE : method from SpriteKit- the right way to multitask doesn't work.

So, in my ViewController.m file, I'm defining private variable SKView *_skView.

Then, I do something like this :

- (void)viewDidLayoutSubviews
{
  [super viewDidLayoutSubviews];

  if(!_skView)
  {
    _skView = [[SKView alloc] initWithFrame:self.view.bounds];
    _skView.showsFPS = NO;
    _skView.showsNodeCount = NO;
    // Create and configure the scene.
    SKScene * scene = [MainMenuScene sceneWithSize:_skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [_skView presentScene:scene];
    [self.view addSubview:_skView];
  }
}

And I have my _skView defined, and everything works just fine.

But, when I interrupt game, it resets its state to initial, so, for example, if I'm currently playing, and somebody calls me, game switches back to main menu. It can't be like this.

Based on the site I mentioned above, I created this :

- (void)applicationWillResignActive:(UIApplication *)application
{
  SKView *view = (SKView *)self.window.rootViewController.view;
  view.paused = YES; 
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  SKView *view = (SKView *)self.window.rootViewController.view;
  view.paused = NO;
}

But game crashes as soon as it is launched, because second method is called and SKView* view is nil. Getting it from self.window.rootViewController.view doesn't work.

I also tried to get it from self.window.rootViewController.view.subviews, but it doesn't work either.

I can't define my SKView (in ViewController.m) like this :

SKView * skView = (SKView *)self.view;

because then I have errors with my GameCenterController.

Can someone help me how correctly get actual skView and pause it properly??


回答1:


You could subscribe to these notifications yourself within the ViewController that manages the SKView. Then you don't have to navigate some weird hierarchy to obtain it.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(willEnterBackground)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(willEnterForeground)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];

- (void)willEnterForeground {
    self.skView.paused = NO;
}

- (void)willEnterBackground {
    // pause it and remember to resume the animation when we enter the foreground
    self.skView.paused = YES;
}


来源:https://stackoverflow.com/questions/22299905/spritekit-how-to-correctly-pause-and-resume-app

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