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

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

问题描述

我使用 Games by Tutorials 一书的帮助制作的最新 iPhone 游戏遇到了大问题.

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

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

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

因此,在我的 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天全站免登陆