How to keep a SpriteKit scene paused when an application becomes active?

In any case, so that SpriteKit does not automatically pause the scene when entering the foreground / becoming active?

I set paused = true and want it to stay that way even when the application reactivates after being sent to the background.

I must add that I am doing this quickly, although I would not expect the behavior to be different in this regard.

+6
source share
4 answers

Not sure if this is the same in a C object, but in swift I had to β€œredefine” the callback function that SKView calls backstage,

 func CBApplicationDidBecomeActive() { } 

This function paused as reset.

(note the override keyword)

In some cases, when you just want to keep the pause state, instead make a new variable and override the isPaused method.

 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 } } } 
+5
source

Pinxaton you're right, but you can pause the application by adding a little delay

  (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; } 
+3
source

this question may have been pretty old, but today I ran into the same problem and I think I found a pretty good solution for it:

In AppDelegate, I do the following:

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

And then in the GameScene class GameScene I update BOOL to reflect that the application has just resumed from the background:

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

And finally, in the update loop: I run the following:

 - (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... } 

Hope this helps!

+2
source

You should use the application delegate, in particular the applicationDidBecomeActive method. In this method, send a notification that listens to your SpriteKit view.

So, in the applicationDidBecomeActive method, your code should look something like this:

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

Now in your didMoveToView method in your SKScene file, add the following:

 // 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]; 

Then just add this method to your SKScene file:

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

Source: https://habr.com/ru/post/973937/


All Articles