繁体   English   中英

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

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

好的,我知道SpriteKit在输入前景和背景时会自动暂停并继续游戏,但是只要应用程序进入后台,我都想进入暂停菜单(这是GameScene中的一种方法)。 我通过在-applicationDidEnterBackground调用该方法来完成此操作,但由于某种原因游戏会崩溃。 正确的方法是什么?

编辑:

这是错误消息:

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)

这是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];
} 

这基本上停止了游戏中的每个进程(只有在isPaused = NO时,所有内容才起作用),并且它模拟了游戏顶部的暂停菜单。

我认为

  1. 您正在填充self.pausedImageself.restartself.resume
  2. 以上3个属性中的至少1个错误地为nil

现在,如果我的假设是正确的(您可以确认吗?),我认为以下情况之一可能是导致崩溃的原因:

  1. 上述3个属性中的至少1个被声明为
  2. 暂停游戏时, 从未调用 填充这3个属性的代码
  3. 您有一些代码将3个属性中的至少1个设置为nil

希望这可以帮助。

暂无
暂无

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

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