4

I was wondering how would i pause my sprite kit scene when home button is pressed.

I found few answers here and tried it with notification center like this.

When my scene load:

 [[NSNotificationCenter defaultCenter]
          addObserver:self
          selector:@selector(applicationDidEnterBackground)
          name:UIApplicationDidEnterBackgroundNotification
          object:nil];

And then later the method that is called if enters to background:

 - (void) applicationDidEnterBackground{
     NSLog(@"Enter to background");
     self.scene.view.paused  =YES;
 }

Problem here is that i get the NSLog message so applicationDidEnterBackground method is being called properly. But problem is that when I return to application my app is not on "pause" mode.

So my pause statement (self.scene.view.paused =YES;) is not being called?

If I put exact statement somewhere else in code or if I make a pause button with this statement pause works just fine.

What is the problem? Why this won't work with notification center?

MOzeb
  • 423
  • 1
  • 6
  • 14

2 Answers2

4

Sprite kit for iOS 8 automatically resumes your game after exiting background mode. It happens after applicationDidBecomeActive is called. Also, Sprite kit for iOS 8 automatically pauses your game when it moves to the background.

Update: The following are the states of skView.paused when enter/exiting background mode for Xcode 5 and 6.

Xcode 6

Deployment targets 7.0, 7.1**, 8.0, and 8.1

applicationWillResignActive = NO
applicationDidEnterBackground = YES
applicationWillEnterForeground = YES
applicationDidBecomeActive = YES

** When I ran on a device running iOS 7.1, the states were all NO

Xcode 5

Deployment targets 7.0 and 7.1

applicationWillResignActive = NO
applicationDidEnterBackground = NO
applicationWillEnterForeground = NO
applicationDidBecomeActive = NO
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • What if the user wants the game to be available for iOS 7 as well? – ZeMoon Nov 08 '14 at 11:29
  • and what if i want to pause anyway? that user is able to push play button by himself? – MOzeb Nov 08 '14 at 14:11
  • I have tried with putting (self.scene.view.paused =YES;) statement in applicationDidBecomeActive when i return to app an it works!! Thanks a lot.. Really useful information! But it is just problem that sometimes apps restarts instead pause? – MOzeb Nov 08 '14 at 14:55
  • @ZeMoon see my edited answer. Matiass21, did you add the 'paused = YES' statement in the app delegate? – 0x141E Nov 09 '14 at 00:34
  • @0x141E hey it works now! But SpriteKit is having problems with this pausing scene. You know sprite kit won't addChild if it is in pause mode. It's logical of course, but it also won't work if i want to addChild when app goes to background or if i want to build one in appWillResignActive.. also it won't work if i addChild at the start of the scene and then just calling it like: pauseButton.hidden = NO; ... So you are not able to build pause or play button to tap it when you want to return to playing... – MOzeb Nov 09 '14 at 09:23
  • 1
    You you will need to pause the view after you add the button. Here's a way to do that [how to add a pause button](http://stackoverflow.com/questions/25609631/spritekit-pause-and-resume-skview/25612551#25612551) – 0x141E Nov 09 '14 at 09:36
  • @0x141E How couldn't I figured out by myself :) Thanks a lot! You have solved all my problems! – MOzeb Nov 09 '14 at 10:03
  • I created a boolean called gamePaused, when I get the notification I set it to YES. In my update: method I set self.paused to YES and return if gamePaused is YES and self.paused = NO if gamePaused is NO – iOSDevil Feb 04 '15 at 11:18
2

By the time your application has entered the background, it's probably too late.

Instead, we should register for the UIApplicationWillResignActiveNotification notification and handle our just-before-exit code when we receive this notification.

nhgrif
  • 61,578
  • 25
  • 134
  • 173