The scene auto pauses on applicationWillResignActive and auto unpauses when applicationDidBecomeActive is run. I am looking to either have the scene pause on applicationWillResignActive via the nsnotification and not auto resume when applicationDidBecomeActive is run. Any ideas? Thanks in advance.
AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[[NSNotificationCenter defaultCenter] postNotificationName:@"backgroundPause" object:nil];
}
GameViewController
- (void)handleNotification:(NSNotification *)notification {
if ([notification.name isEqualToString:@"backgroundPause"]) {
SKView *skView = (SKView *)self.view;
skView.scene.paused = YES; //pauses scene
[self.lblPaused removeFromSuperview];//removes any lingering pause menu items
[self.lblPausedHelp removeFromSuperview];
self.lblPaused = [[UILabel alloc] init];
self.lblPaused.center = CGPointMake(self.view.frame.size.width/2 - 125, self.view.frame.size.height/2 - 40);
self.lblPaused.text = @"PAUSED";
[self.lblPaused setFont:[UIFont boldSystemFontOfSize:66]];
[self.lblPaused sizeToFit];
self.lblPaused.textColor = [UIColor blackColor];
[self.view addSubview:self.lblPaused];//adds pause label
self.lblPausedHelp = [[UILabel alloc] init];
self.lblPausedHelp.center = CGPointMake(self.view.frame.size.width/2 - 145, self.view.frame.size.height/2 + 40);
self.lblPausedHelp.text = @"tap anywhere to resume";
[self.lblPausedHelp setFont:[UIFont boldSystemFontOfSize:26]];
[self.lblPausedHelp sizeToFit];
self.lblPausedHelp.textColor = [UIColor blackColor];
[self.view addSubview:self.lblPausedHelp];//adds pause label
}
}
I'm pretty sure it's a bug in spritekit. No matter what you do, the game will unpause itself in applicationDidBecomeActive
I asked the same question here. pausing spritekit game on app launch / exit .. iOS8 You have to subclass SKScene and override the paused
property to get it to work. It's weird you have to do that. It really shouldnt have so many problems, but thats the only way I could get my game to stay paused
EDIT: okay, I translated the code to objective-c. I hope this is useful to you because my objective-c was rustier than i expected.
AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter]postNotificationName:@"pauseGameScene" object:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter]postNotificationName:@"stayPausedNotification" object:nil];
}
SKView Subclass
@interface MySKView : SKView
- (void) setStayPaused;
@end
@implementation MySKView
bool _stayPaused = false;
- (void) setPaused:(BOOL)paused{
if (!_stayPaused) {
super.paused = paused;
}
_stayPaused = NO;
}
- (void) setStayPaused{
_stayPaused = YES;
}
@end
GameViewController
@interface GameViewController : UIViewController
-(void)pauseGame;
@end
@implementation GameViewController
SKScene *_scene;
MySKView *_skView;
-(void)pauseGame{
_skView.paused = YES;
_skView.scene.view.paused = YES;
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pauseGame) name:@"pauseGameScene" object:nil];
// Configure the view.
_skView = [[MySKView alloc]initWithFrame:self.view.frame];
_skView.showsFPS = YES;
_skView.showsNodeCount = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
_skView.ignoresSiblingOrder = YES;
// Create and configure the scene.
_scene = [[GameScene alloc]initWithSize:_skView.frame.size];
_scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[self.view addSubview:_skView];
[_skView presentScene:_scene];
}
- (void)viewDidAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]addObserver:_skView selector:@selector(setStayPaused) name:@"stayPausedNotification" object:nil];
}
@end