didBecomeActive未暂停比赛
问题描述:
我停下我的比赛有willResignActive通知,似乎暂停了比赛,但是当didBecomeActive被调用时,它似乎对自己取消暂停。didBecomeActive未暂停比赛
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationWillResign)
name:UIApplicationWillResignActiveNotification
object:NULL];
- (void) applicationWillResign {
self.scene.view.paused = TRUE;
NSLog(@"About to lose focus");
}
如何让它保持暂停?我是否真的需要在AppDelegate中暂停它?
答
这里有一个方法来保持视图从后台模式返回后暂停。这有点破解,但确实有效。
1)定义的子类SKView与命名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的
3)在视图控制器,定义视图作为MyView的
4)添加通知设置stayPaused标志
@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中,发布通知,以在应用变为活动状态时设置停留暂停标志
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:@"stayPausedNotification" object:nil];
}
该代码来自另一个问题。 – Siriss 2014-10-12 02:45:34
显然,在Xcode 6,子画面试剂盒时自动应用程式失去焦点并恢复(未暂停)时,应用程序进入前景暂停场景和视图。在调用applicationDidBecomeActive之后,Sprite工具包会恢复游戏,所以我不认为向AppDelegate添加代码将起作用。 – 0x141E 2014-10-12 03:01:11
因此,我必须确保它在恢复时仍处于暂停状态? – Siriss 2014-10-12 03:11:15