应用程序变为活动状态时如何保持SpriteKit场景暂停?

有没有办法阻止SpriteKit在进入前景/活动时自动取消暂停场景?

我设置了paused = true并希望它保持如此,即使应用程序被发送到后台后再次激活。

我应该补充一点,我很快就做到了,尽pipe我不希望在这方面的行为有所不同。

不知道在目标C中它是否一样,但在Swift中,我不得不“重写”一个SKView在幕后调用的callback函数,

 func CBApplicationDidBecomeActive() { } 

此function导致暂停重置。

(注意覆盖关键字不应该被应用)

在某些情况下,如果您只想保留暂停状态,请改为创build一个新variables并覆盖isPaused方法。

 class GameScene:SKScene { var realPaused = false { didSet { isPaused = realPaused } } override var isPaused : Bool { get { return realPaused } set { //we do not want to use newValue because it is being set without our knowledge paused = realPaused } } } 

Pinxaton你是对的,但你可以通过添加一个小的延迟暂停应用程序

  (void)theAppIsActive:(NSNotification *)note { self.view.paused = YES; SKAction *pauseTimer= [SKAction sequence:@[ [SKAction waitForDuration:0.1], [SKAction performSelector:@selector(pauseTimerfun) onTarget:self] ]]; [self runAction:pauseTimer withKey:@"pauseTimer"]; } -(void) pauseTimerfun { self.view.paused = YES; } 

这个问题可能已经很老了,但我今天遇到了同样的问题,我想我已经find了一个很好的解决scheme:

在AppDelegate中,我执行以下操作:

 - (void)applicationDidBecomeActive:(UIApplication *)application { SKView *view = (SKView *)self.window.rootViewController.view; if ([view.scene isKindOfClass:[GameScene class]]) { XGGameScene *scene = (GameScene *)view.scene; [scene resumeGame]; } } 

然后在GameScene类本身中,我更新BOOL以反映应用程序刚刚从后台恢复:

 - (void)resumeGame { // Do whatever is necessary self.justResumedFromBackground = YES; } 

最后,在update:loop中,我运行以下命令:

 - (void)update:(NSTimeInterval)currentTime { // Other codes go here... // Check if just resumed from background. if (self.justResumedFromBackground) { self.world.paused = YES; self.justResumedFromBackground = NO; } // Other codes go here... } 

希望这可以帮助!

您应该使用您的应用程序委托,特别是applicationDidBecomeActive方法。 在该方法中,发送一个SpriteKit视图侦听的通知。

所以在applicationDidBecomeActive方法中,你的代码应该看起来像这样:

 // Post a notification when the app becomes active [[NSNotificationCenter defaultCenter] postNotificationName:@"appIsActive" object:nil]; 

现在在你的SKScene文件的didMoveToView方法中放置如下:

 // Add a listener that will respond to the notification sent from the above method [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(theAppIsActive:) name:@"appIsActive" object:nil]; 

然后,只需将此方法添加到您的SKScene文件中:

 //The method called when the notification arrives (void)theAppIsActive:(NSNotification *)note { self.view.paused = YES; }