简体   繁体   English

如何杀死SKScene

[英]How to kill an SKScene

I've got a game with a MasterViewController. 我有一个使用MasterViewController的游戏。 It has a "Play" button that segues to the GameViewController. 它具有一个“播放”按钮,可与GameViewController相连。 The GameViewController and GameScene are plain vanilla what a game build provides except I added an NSLog to the GameScene's update method and, on the storyboard, created a "Quit" button that segues back from the GameViewController to the MasterViewController. GameViewController和GameScene是游戏版本提供的普通香草,除了我在GameScene的update方法中添加了NSLog并在情节提要上创建了一个“退出”按钮,该按钮从GameViewController返回到MasterViewController。

Everything works as expected. 一切正常。 I fire up the app and touch the Play button and it transitions to the GameViewController to the GameScene. 我启动该应用程序,然后触摸“播放”按钮,它将过渡到GameViewController到GameScene。 Fine, I see the standard "Hello World" message and can touch to create spinning spaceships. 很好,我看到标准的“ Hello World”消息,可以触摸以创建旋转的宇宙飞船。 I start getting the NSLog output from the update method. 我开始从更新方法获取NSLog输出。 Great. 大。

However, when I click the Quit button and it segues back to the MasterViewController, I am still getting the NSLog output from the GameScene update method so the GameScene is still active. 但是,当我单击“退出”按钮并重新回到MasterViewController时,我仍然从GameScene更新方法获取NSLog输出,因此GameScene仍处于活动状态。 I want the GameScene gone. 我希望GameScene消失了。 Added a dealloc to the GameScene and it is never called, probably because of ARC. 在GameScene中添加了一个dealloc,它从未被调用过,这可能是由于ARC所致。

In the GameViewController I added a weak gameScene property and: 在GameViewController中,我添加了一个弱的gameScene属性,并:

- (void)viewWillDisappear:(BOOL)animated {
   [super viewWillDisappear:animated];
   NSLog(@"viewWillDisappear");
   [_gameScene removeAllChildren];
   [_gameScene removeAllActions];
   [_gameScene removeFromParent];
   _gameScene = nil;
}

Still getting the NSLog output from the GameScene update method. 仍从GameScene更新方法获取NSLog输出。 Sigh... How do I kill the GameScene dead, dead, dead? 叹息...我该如何杀死GameScene,死了,死了,死了?

I did the Play/Quit/Play/Quit transition several times. 我做了几次“播放/退出/播放/退出”转换。 The output from the update method is: 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

So all of my GameScenes are still active in the background. 因此,我所有的GameScenes仍在后台处于活动状态。

You must make sure no other objects are pointing strongly to the object you want to remove from memory. 您必须确保没有其他对象强烈指向要从内存中删除的对象。 See Apple Developer . 请参阅Apple Developer

I found a workaround - use a nav controller, which I alway hide to make room for my game. 我找到了一种解决方法-使用导航控制器,我总是将其隐藏以为我的游戏腾出空间。 To transition to the next view controller in the hierarchy, use a "Show" segue. 要过渡到层次结构中的下一个视图控制器,请使用“显示”按钮。 To pop back, add your own Back button and connect the action: 要弹出,请添加自己的“后退”按钮并连接操作:

- (IBAction)backButtonClicked:(UIButton *)sender {
   [[self navigationController] popViewControllerAnimated:YES];
}

I also have one place in my GameViewController where I use a "Pause" button to pop all the way back to the root view controller. 我在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];
}

All this correctly deallocates the GameViewController and GameScene when they disappear. 当它们消失时,所有这些正确地分配了GameViewController和GameScene。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM