0
votes

I am attempting to pause the game when the user navigates away from the app or clicks on the ad using the following code:

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

This used to work in pausing the game, but due to some other stuff going on, I ended up adding another viewcontroller to handle the title scene, and got everything working, but for some reason the pause code now doesn't work and I get the following error:

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

Note that I am still able to pause within my scene using

self.scene.view.paused = YES;

Does anyone know why this might be happening? Through reading some of the other related questions, I can see that it might be useful to use NSNotification? Another option is to add an observer? Does anyone know why the pause code I am using is not working from the appDelegate anymore and what I can do to fix this?


3

3 Answers

1
votes

Add a new method in 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;
}

and now ... modify your code from:

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

to:

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

Most likely that either rootViewController is not the ViewController, which operates SKView, or rootViewController.view is not SKView.

Use NSNotificationCenter instead of trying to pause the game directly from 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

And then just call

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

Modify your code in AppDelegate:

  • WillResignActive:

    -(void)applicationWillResignActive:(UIApplication *)application {
    
        YourViewController *viewGame = [[UIStoryboard storyboardWithName: StoryboardName bundle: nil] instantiateViewControllerWithIdentifier: identifier];
    
        SKView *view = (SKView *)viewGame.view.window;
        view.paused = YES;
    
    }
    
  • and in 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;
       }
    }