按下home键时暂停Sprite工具包场景

时间:2023-01-22 22:30:42

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

我想知道当按下home键时,我该如何暂停我的sprite kit场景。

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:

然后调用if的方法进入后台:

 - (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.

这里的问题是,我得到了NSLog消息,因此applicationDidEnterBackground方法被正确地调用。但问题是,当我回到应用程序时,我的应用程序并没有处于“暂停”模式。

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

我的暂停语句(self。scene.view)暂停=是;)不被调用吗?

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?

这个问题是什么?为什么这在通知中心不起作用?

2 个解决方案

#1


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.

在退出后台模式后,ios8的Sprite工具包会自动恢复你的游戏。它发生在调用applicationDidBecomeActive之后。此外,iOS 8的Sprite工具包在游戏移动到后台时自动暂停游戏。

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

更新:以下是skView的状态。当Xcode 5和6进入/退出后台模式时暂停。

Xcode 6

Xcode 6

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

部署目标为7.0、7.1**、8.0和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

**当我在运行iOS 7.1的设备上运行时,所有的状态都不是。

Xcode 5

Xcode 5

Deployment targets 7.0 and 7.1

部署目标为7.0和7.1

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

#2


1  

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.

相反,我们应该注册UIApplicationWillResignActiveNotification通知,并在收到通知时处理我们刚退出前的代码。

#1


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.

在退出后台模式后,ios8的Sprite工具包会自动恢复你的游戏。它发生在调用applicationDidBecomeActive之后。此外,iOS 8的Sprite工具包在游戏移动到后台时自动暂停游戏。

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

更新:以下是skView的状态。当Xcode 5和6进入/退出后台模式时暂停。

Xcode 6

Xcode 6

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

部署目标为7.0、7.1**、8.0和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

**当我在运行iOS 7.1的设备上运行时,所有的状态都不是。

Xcode 5

Xcode 5

Deployment targets 7.0 and 7.1

部署目标为7.0和7.1

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

#2


1  

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.

相反,我们应该注册UIApplicationWillResignActiveNotification通知,并在收到通知时处理我们刚退出前的代码。