按下主屏幕按钮时暂停Sprite Kit场景 [英] Pause Sprite Kit scene when home button is pressed
问题描述
我想知道当按下主页按钮时如何暂停我的Sprite Kit场景.
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.
当我的场景加载时:
[[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;
}
这里的问题是我收到了NSLog消息,因此applicationDidEnterBackground方法被正确调用.但是问题在于,当我返回应用程序时,我的应用程序未处于暂停"模式.
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.
所以我的暂停语句(self.scene.view.paused = YES;)没有被调用吗?
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?
推荐答案
iOS 8的Sprite套件在退出后台模式后会自动恢复游戏.它在调用applicationDidBecomeActive之后发生.此外,iOS 8的Sprite套件会在移动到后台时自动暂停游戏.
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.
更新:以下是进入/退出Xcode 5和6的后台模式时skView.paused的状态.
Update: The following are the states of skView.paused when enter/exiting background mode for Xcode 5 and 6.
Xcode 6
部署目标是7.0、7.1 **,8.0和8.1
Deployment targets 7.0, 7.1**, 8.0, and 8.1
applicationWillResignActive = NO
applicationDidEnterBackground = YES
applicationWillEnterForeground = YES
applicationDidBecomeActive = YES
**当我在运行iOS 7.1的设备上运行时,状态均为否
** When I ran on a device running iOS 7.1, the states were all NO
Xcode 5
部署目标7.0和7.1
Deployment targets 7.0 and 7.1
applicationWillResignActive = NO
applicationDidEnterBackground = NO
applicationWillEnterForeground = NO
applicationDidBecomeActive = NO
这篇关于按下主屏幕按钮时暂停Sprite Kit场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!