Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...