[英]How to kill an SKScene
我有一個使用MasterViewController的游戲。 它具有一個“播放”按鈕,可與GameViewController相連。 GameViewController和GameScene是游戲版本提供的普通香草,除了我在GameScene的update方法中添加了NSLog並在情節提要上創建了一個“退出”按鈕,該按鈕從GameViewController返回到MasterViewController。
一切正常。 我啟動該應用程序,然后觸摸“播放”按鈕,它將過渡到GameViewController到GameScene。 很好,我看到標准的“ Hello World”消息,可以觸摸以創建旋轉的宇宙飛船。 我開始從更新方法獲取NSLog輸出。 大。
但是,當我單擊“退出”按鈕並重新回到MasterViewController時,我仍然從GameScene更新方法獲取NSLog輸出,因此GameScene仍處於活動狀態。 我希望GameScene消失了。 在GameScene中添加了一個dealloc,它從未被調用過,這可能是由於ARC所致。
在GameViewController中,我添加了一個弱的gameScene屬性,並:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"viewWillDisappear");
[_gameScene removeAllChildren];
[_gameScene removeAllActions];
[_gameScene removeFromParent];
_gameScene = nil;
}
仍從GameScene更新方法獲取NSLog輸出。 嘆息...我該如何殺死GameScene,死了,死了,死了?
我做了幾次“播放/退出/播放/退出”轉換。 update方法的輸出為:
2014-11-20 12:48:41.551 Demo[7386:2004098] update: 0x7b091090
2014-11-20 12:48:42.095 Demo[7386:2004098] update: 0x7ed21020
2014-11-20 12:48:42.656 Demo[7386:2004098] update: 0x7eb1c4b0
2014-11-20 12:48:43.217 Demo[7386:2004098] update: 0x7b091090
2014-11-20 12:48:43.762 Demo[7386:2004098] update: 0x7ed21020
2014-11-20 12:48:44.322 Demo[7386:2004098] update: 0x7eb1c4b0
因此,我所有的GameScenes仍在后台處於活動狀態。
您必須確保沒有其他對象強烈指向要從內存中刪除的對象。 請參閱Apple Developer 。
我找到了一種解決方法-使用導航控制器,我總是將其隱藏以為我的游戲騰出空間。 要過渡到層次結構中的下一個視圖控制器,請使用“顯示”按鈕。 要彈出,請添加自己的“后退”按鈕並連接操作:
- (IBAction)backButtonClicked:(UIButton *)sender {
[[self navigationController] popViewControllerAnimated:YES];
}
我在GameViewController中也有一個位置,使用“暫停”按鈕將所有位置彈出回到根視圖控制器。
- (IBAction)pauseButtonClicked:(UIButton *)sender {
NSArray * viewControllers = self.navigationController.viewControllers;
NSLog(@"nav view controllers: %@", viewControllers);
UIViewController * targetViewController = viewControllers[0];
NSLog(@"target controller: %@", targetViewController);
[self.navigationController popToViewController:targetViewController animated:YES];
}
當它們消失時,所有這些正確地分配了GameViewController和GameScene。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.