簡體   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