【问题标题】:Spritekit game crashes when pausing scene from app delegate从应用委托暂停场景时,Spritekit 游戏崩溃
【发布时间】:2014-08-19 20:10:54
【问题描述】:

当用户离开应用程序或使用以下代码点击广告时,我正在尝试暂停游戏:

 SKView *view = (SKView *)self.window.rootViewController.view;
    view.paused = YES;

这曾经在暂停游戏时起作用,但由于发生了一些其他事情,我最终添加了另一个视图控制器来处理标题场景,并且一切正常,但由于某种原因,暂停代码现在不起作用我收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setPaused:]: unrecognized selector sent to instance 0x170174280'

请注意,我仍然可以使用

在我的场景中暂停
self.scene.view.paused = YES;

有谁知道为什么会发生这种情况?通过阅读其他一些相关问题,我可以看到使用 NSNotification 可能有用吗?另一种选择是添加观察者?有谁知道为什么我使用的暂停代码不再适用于 appDelegate 以及我能做些什么来解决这个问题?


【问题讨论】:

    标签: ios sprite-kit


    【解决方案1】:

    在 appDelegate 中添加一个新方法:

    - (SKView *)getGameView {
        NSArray *viewControllers = self.window.rootViewController.childViewControllers;
        for (UIViewController *vc in viewControllers) {
            if ([vc.view isKindOfClass:[SKView class]]) {
                SKView *view = (SKView *)vc.view;
                return view;
            }
        }
        return nil;
    }
    

    现在...修改您的代码:

    SKView *view = (SKView *)self.window.rootViewController.view;
    view.paused = YES;
    

    到:

    SKView *view = [self getGameView];
       if (view) {
           view.paused = YES; //or NO
          }
    

    【讨论】:

      【解决方案2】:

      很可能rootViewController 不是ViewController,它运行SKView,或者rootViewController.view 不是SKView

      使用NSNotificationCenter 而不是尝试直接从AppDelegate 暂停游戏:

      @interface GameSceneViewController
      
      @property (nonatomic, weak) IBOutlet SKView *skView;
      
      @end
      
      @implementation GameSceneViewController
      
      - (void)viewDidLoad {
          [super viewDidLoad];
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pause) name:@"pauseView" object:nil];
      }
      - (void)pause {
          self.skView.pause = YES;
      }
      
      @end
      

      然后只需调用

      [[NSNotificationCenter defaultCenter] postNotificationName:@"pauseView" object:nil];
      

      【讨论】:

      • 我添加了这段代码,但它似乎不起作用。我在“applicationDidEnterBackground”方法下调用了 App Delegate 中的最后一行,但当我离开应用程序时仍然出现 EXC_BAD_ACCESS 错误。我怀疑除了暂停逻辑之外可能还会发生一些事情,但我不知道它可能是什么。
      【解决方案3】:

      在 AppDelegate 中修改您的代码:

      • WillResignActive:

        -(void)applicationWillResignActive:(UIApplication *)application {
        
            YourViewController *viewGame = [[UIStoryboard storyboardWithName: StoryboardName bundle: nil] instantiateViewControllerWithIdentifier: identifier];
        
            SKView *view = (SKView *)viewGame.view.window;
            view.paused = YES;
        
        }
        
      • 在 DidBecomeActive 中:

        - (void)applicationDidBecomeActive:(UIApplication *)application {
        
        if (!self.window.rootViewController.view)  {
            YourViewController *viewGame = [[UIStoryboard storyboardWithName: StoryboardName bundle: nil]instantiateViewControllerWithIdentifier: identifier];
        
            SKView *view = (SKView *)viewGame.view.window;
            view.paused = NO;
           }
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多