【问题标题】:SpriteKit - how to correctly pause and resume appSpriteKit - 如何正确暂停和恢复应用程序
【发布时间】:2014-04-13 12:58:43
【问题描述】:

我在使用 Games by Tutorials 书籍的帮助制作的最新 iPhone 游戏时遇到了很大的问题。

注意:SpriteKit- the right way to multitask 的方法不起作用。

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

然后,我做这样的事情:

- (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,一切正常。

但是,当我中断游戏时,它会将其状态重置为初始状态,因此,例如,如果我正在玩游戏并且有人打电话给我,游戏会切换回主菜单。不可能是这样的。

基于我上面提到的网站,我创建了这个:

- (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 获取它不起作用。

我也尝试从 self.window.rootViewController.view.subviews 中获取,但也没有用。

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

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

因为我的 GameCenterController 出现错误。

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

【问题讨论】:

    标签: ios objective-c view sprite-kit skview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-25
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2017-11-17
      相关资源
      最近更新 更多