简体   繁体   中英

Pause Sprite Kit scene when home button is pressed

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM