简体   繁体   English

当应用程序进入后台时,如何在SpriteKit中正确暂停?

[英]How do I properly pause in SpriteKit when the application goes to background?

Ok, I know that SpriteKit automatically pauses and resumes games when the enter foregrounds and backgrounds, but I want to go to the pause menu (which is a method in my GameScene) whenever the application goes to the background. 好的,我知道SpriteKit在输入前景和背景时会自动暂停并继续游戏,但是只要应用程序进入后台,我都想进入暂停菜单(这是GameScene中的一种方法)。 I do this by calling that method in -applicationDidEnterBackground , but for some reason the game crashes. 我通过在-applicationDidEnterBackground调用该方法来完成此操作,但由于某种原因游戏会崩溃。 What is the proper way to do this? 正确的方法是什么?

EDIT: 编辑:

This is the error message: 这是错误消息:

Terminating app due to uncaught exception 'Attemped to add nil node', reason: 'Attemped to add nil node to parent: <SKScene> name:'(null)' frame:{{0, 0}, {1, 1}}'
*** First throw call stack:
(0x181d2259c 0x1924780e4 0x181d224dc 0x18635812c 0x10003dd90 0x10003e818 0x1000394b0 0x1867627c0 0x18676acbc 0x18676ac44 0x18675e578 0x189f9162c 0x181cdaa28 0x181cd9b30 0x181cd7d30 0x181c050a4 0x18ada75a4 0x18653a3c0 0x10003f700 0x192ae6a08)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

This is what the pauseGame method does: 这是pauseGame方法的作用:

NSLog(@"Pausing...");
[self removeActionForKey:@"spawn"];
[self addChild:self.pausedImage];
[self addChild:self.restart];
[self addChild:self.resume];

NSString *path = [NSString stringWithFormat:@"%@/menu.mp3", [[NSBundle mainBundle]resourcePath]];
NSURL *pauseMusicURL = [NSURL fileURLWithPath:path];
self.pauseMusicPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:pauseMusicURL error:nil];
self.pauseMusicPlayer.numberOfLoops = -1;
[self.pauseMusicPlayer play];

[self.pause removeFromParent];
self.scoreLabelInGame.position = CGPointMake(self.restart.position.x, self.resume.position.y - self.customUnit);
self.actualScore.position = CGPointMake(self.restart.position.x, self.scoreLabelInGame.position.y - self.customUnit);
self.isPaused = YES;
[self.mainMusicPlayer pause];
} 

This basically stops every process in the game (Everything only works if isPaused = NO) And it simulates a pause menu on top of the game. 这基本上停止了游戏中的每个进程(只有在isPaused = NO时,所有内容才起作用),并且它模拟了游戏顶部的暂停菜单。

I assume that 我认为

  1. you are populating self.pausedImage , self.restart , self.resume 您正在填充self.pausedImageself.restartself.resume
  2. at least 1 of the 3 properties above is erroneously nil 以上3个属性中的至少1个错误地为nil

Now if my assumptions are correct (can you confirm?), I think one of the following scenarios can be the cause of the crash: 现在,如果我的假设是正确的(您可以确认吗?),我认为以下情况之一可能是导致崩溃的原因:

  1. at least 1 of the 3 properties mentioned above are declared as weak 上述3个属性中的至少1个被声明为
  2. the code that populates these 3 properties has never been called when you pause the game 暂停游戏时, 从未调用 填充这3个属性的代码
  3. you have some code that set to nil at least 1 of the 3 properties 您有一些代码将3个属性中的至少1个设置为nil

Hope this helps. 希望这可以帮助。

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

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