SpriteKit - 如何正确暂停和恢复应用程序 [英] SpriteKit - how to correctly pause and resume app

查看:154
本文介绍了SpriteKit - 如何正确暂停和恢复应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最新的iPhone游戏使用由教程提供的游戏帮助。

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

注意:来自 SpriteKit-正确的多任务方式不起作用。

所以,在我的ViewController.m文件中,我正在定义私有变量SKView * _skView。

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

我定义了_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;
}

但游戏一启动就会崩溃,因为第二种方法被调用, SKView *视图为零。
从self.window.rootViewController.view获取它不起作用。

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.

我也尝试从self.window.rootViewController.view.subviews获取它,但它也不起作用。

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

我不能像这样定义我的SKView(在ViewController.m中):

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

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

因为我的GameCenterController出错了。

because then I have errors with my GameCenterController.

有人可以帮我正确获取实际的skView并正确暂停吗?

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

推荐答案

您可以自己订阅这些通知在管理SKView的ViewController中。然后你不必导航一些奇怪的层次结构来获得它。

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

这篇关于SpriteKit - 如何正确暂停和恢复应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆