Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit - how to correctly pause and resume app

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

like image 689
Artur Bartczak Avatar asked Mar 10 '14 12:03

Artur Bartczak


1 Answers

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;
}
like image 109
jervine10 Avatar answered Oct 27 '22 06:10

jervine10