didBecomeActive非暂停游戏 [英] didBecomeActive un-pauses game
问题描述
我正在使用willResignActive通知暂停我的游戏,它似乎暂停了游戏,但是当调用didBecomeActive时,它似乎会自行取消暂停.
I am pausing my game with a willResignActive notification, and it seems to pause the game, but when didBecomeActive is called, it seems to un-pause on its own.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationWillResign)
name:UIApplicationWillResignActiveNotification
object:NULL];
- (void) applicationWillResign {
self.scene.view.paused = TRUE;
NSLog(@"About to lose focus");
}
如何使其保持暂停状态?我是否真的需要在AppDelegate中将其暂停?
How do I get it to stay paused? Do I actually need to pause it in my AppDelegate?
推荐答案
这是从后台模式返回后保持视图暂停的一种方法.有点hack,但确实可以.
Here's a way to keep the view paused after returning from background mode. It's a bit of a hack, but it does work.
1)用一个名为stayPaused的布尔值定义一个SKView子类...
1) Define an SKView subclass with a boolean named stayPaused...
@interface MyView : SKView
@property BOOL stayPaused;
@end
@implementation MyView
// Override the paused setter to conditionally un-pause the view
- (void) setPaused:(BOOL)paused
{
if (!_stayPaused || paused) {
// Call the superclass's paused setter
[super setPaused:paused];
}
_stayPaused = NO;
}
- (void) setStayPaused
{
_stayPaused = YES;
}
@end
2)在情节提要中,将视图的类更改为MyView
2) In the storyboard, change the class of the view to MyView
3)在视图控制器中,将视图定义为MyView
3) In the view controller, define the view as MyView
4)添加通知程序以设置stayPaused标志
4) Add a notifier to set the stayPaused flag
@implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Define view using the subclass
MyView * skView = (MyView *)self.view;
// Add an observer for a method that sets the stay pause flag when notified
[[NSNotificationCenter defaultCenter] addObserver:skView selector:@selector(setStayPaused)
name:@"stayPausedNotification" object:nil];
...
5)在AppDelegate.m中,发布通知以设置应用程序处于活动状态时的保持暂停"标志
5) In AppDelegate.m, post a notification to set the stay paused flag when the app becomes active
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:@"stayPausedNotification" object:nil];
}
这篇关于didBecomeActive非暂停游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!