Is there anyway to prevent SpriteKit from automatically unpausing a scene when entering foreground/becoming active?
I set paused = true
and want it to remain so even when the app becomes active again after having been sent to the background.
I should add that I'm doing this in swift, though I would not have expected the behaviour to be different in this regard.
Not sure if it is the same in objective C, but in swift I had to "override" a callback function that SKView calls behind the scenes,
This function was causing paused to be reset.
(note override keyword should not be applied)
In some cases where you want to just retain the state of pause, make a new variable instead and override the isPaused method.
I made some adaptions to the solution from Knight0fDragon to make it work for me. This makes it so that isPaused will always be equal to realPaused. In order to pause the game, the "realPaused" variable should then only be altered, which changes automatically also the isPaused variable.
Unfortunately, this will prevent the scene from pausing when the application is running in the background. In order to prevent that, I changed the condition to: "self.isPaused != self.realPaused && self.isPaused == false" so that the scene will still pause automatically when the app is put to the background but will only re-actiate if realPaused is also true:
Pinxaton you are right but you can paused application by adding a small delay
You should make use of your app delegate, specifically the applicationDidBecomeActive method. In that method send a notification that your SpriteKit view listens for.
So in the applicationDidBecomeActive method your code should look something like this:
Now in your didMoveToView method in your SKScene file put the following:
Then just add this method to your SKScene file:
this question may have been quite old, but I encountered the same problem today and I think I have found a pretty good solution to it:
In the AppDelegate, I do the following:
And then in the
GameScene
class itself, I update a BOOL to reflect that the app has just resumed from background:And finally, in the update: loop, I run the following:
Hope this helps!