0

我需要在游戏进入后台之前暂停游戏,所以当再次进入前台时,我会在游戏中看到暂停对话框。为了显示暂停对话框,我有一个场景方法。如何在应用程序进入后台之前调用场景方法?我看到以下方法是正确的地方:

-(void) applicationDidEnterBackground:(UIApplication*)application
{
    if( [navController_ visibleViewController] == director_ )
        [director_ stopAnimation];
}

我应该从 director 那里得到场景runningScene,将它投射到我的场景类中,然后向它发送暂停消息还是那样很难看?

4

1 回答 1

0

在这种情况下,我认为通知是最干净和最安全的选择。您可以从 applicationDidEnterBackground 发布通知并接收您想要执行某些操作的任何位置,例如:

在应用委托中:

- (void)applicationDidEnterBackground:(UIApplication*)application {
   [[NSNotificationCenter defaultCenter] postNotificationName:@"EnteringBackground" object:nil];
}

在游戏场景中:

- (void)onEnter {
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showPausePopup) name:@"EnteringBackground" object:nil];
}

- (void)onExit {
   [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)showPausePopup:(NSNotification*)notification {
   // Code to show popup
}
于 2013-11-01T23:21:23.850 回答