Pause Sprite Kit scene when home button is pressed

半城伤御伤魂 提交于 2019-12-19 22:01:55

问题


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?


回答1:


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



回答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.



来源:https://stackoverflow.com/questions/26812269/pause-sprite-kit-scene-when-home-button-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!