2014-09-26 73 views
1

我在我的遊戲中有一個暫停菜單,當我點擊主頁按鈕時,我想要切換。它在我處於我的遊戲中時有效,但是當我點擊我的主頁按鈕時,菜單出現,但遊戲不會暫停。在ios8中使用appdelegate暫停spritekit遊戲

當我重新啓動應用程序時,菜單仍然存在,但遊戲未暫停。看起來spritekit會在你離開並進入應用程序時自動暫停並重新啓動遊戲。當我在應用程序內時,我可以完全控制它。但是,當我退出/進入應用程序時,它表現得如何。

有什麼建議嗎?

func applicationWillResignActive(application: UIApplication!) { 
    // get the root viewcontroller 
    // toggle my pausemenu method. pause menu sets skview.paused = true 
} 

回答

1

發送NSNotificationGameSceneViewController暫停SKScene

的AppDelegate:

- (void)applicationWillResignActive:(UIApplication *)application 
{  
    // Pause sprite kit scene 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PauseGameScene" object:self]; 
} 

GameSceneViewController:

- (void)viewDidLoad { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(pauseGameScene) 
               name:@"PauseGameScene" 
               object:nil]; 
} 

- (void)pauseGameScene { 
    if (self.skView.scene) { 
     self.skView.paused = self.skView.scene.paused = YES; 
    } 
} 
+0

你試過這個代碼?它不起作用。遊戲將在重新回到應用程序時自動重啓 – hamobi 2014-10-11 17:34:12

+0

如果遊戲在重新回到應用程序時重新啓動,那麼問題肯定存在於您的項目架構中,而不是我已發佈的代碼中。它工作得很好。 – 2014-10-12 03:09:52

+0

我嘗試了一個基本的spritekit項目並使用這段代碼。它不適用於當前的xcode/ios。你在舊版本嗎? – hamobi 2014-10-12 07:30:37