3

Swift 2.3, SpriteKit

I know about AppDelegate Functions: applicationWillResignActive and applicationDidEnterBackground

I have the following code:

func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

        mainScene.view?.paused = true

}

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

        mainScene.view?.paused = true

}

With applicationDidEnterBackground it works fine. I'm able to pause the scene by tapping Home Button and resume it after.

But with applicationWillResignActive it doesn't work. When I run my game and receive Phone Call, the game doesn't pause. It keeps running in the background, so when I'm done with call and go back, I can see all my nodes in a completely mess.

before willResignActive

After

I used print statement to check if applicationWillResignActive works when I receive a phone Call and it shows me that it is executing, but the app just doesn't pause.

Also I've noticed that when I tap Home Button the app call applicationWillResignActive and then applicationDidEnterBackground functions. When I receive a phone call only applicationWillResignActive function is called.

So my question is - how can I deal with phone calls and pause my App with applicationWillResignActive function. I don't understand why I can pause it when I tap Home Button and can't when I get a Phone Call.

  • You need to add more info. In standard cases, when your app is about to go to background or there is an incoming phone call , a scene will be paused / unpaused automatically (I can show you a simple workable example). I said in standard situations, means using SKActions and update: method, not NSTimer for example to spawn enemies and such... So, try to describe what is happening on your scene at the moment where you trying to pause it and why do you pause the view instead of a scene? – Whirlwind Dec 17 '16 at 23:35
  • I mean why pause at all in this situations, because everything should pause / unpause automatically when going to / returing from a background. The only problem which arises here might be the automatic unpausing, which is sometimes undesired behaviour, but that is another story and not a part of a question :) – Whirlwind Dec 17 '16 at 23:39
  • Thanks for your reply. This is my first app. I don't use NSTimer, only SKActions. I don't know what i'm doing wrong but my app didn't pause/unpause automatically when I've tried, so I had to force it to pause in applicationDidEnterBackground. – user6667443 Dec 18 '16 at 00:32
  • Hm. Okay. But define "didn't pause" ... What is going on on the scene when you receive a phone call, and what is happening when you return to the foreground. If you describe that I could try to guess what is the issue, without the rest of the code, so I can point you where to look for the error. – Whirlwind Dec 18 '16 at 00:34
  • There are two viewControllers with two scenes. One for Menu, one for GameScene. I save the GameScene in the global var - "mainScene" so that I could use it in different classes among swift files. This global var I pause/unpause in the "didEnterBackground". I have to pause View cause if I pause only Scene (like mainScene.pause) I have the same problem like with didResignActive Function. By problems I mean: for example, there are backgrounds that I move with SKAction.moveByX. If i didn't pause the view, than they just keep scrolling in the background. – user6667443 Dec 18 '16 at 00:47
  • or my enemy nodes. I use repeatActionForever to create them and when if I didn't pause the view, they keep spawn, but skaction.MoveByX isn't working for them, so they all stand in one place. It is like all actions that have been created before I tapped Home Button are still active, but new actions that supposed to be there - don't work, so the whole scene is in a mess. – user6667443 Dec 18 '16 at 00:52
  • For setup like you've described, you should have only one view (SKView) IMO, and to have multiple scenes. Like this : http://stackoverflow.com/a/35409586/3402095. So first try to implement something like that WITHOUT global scene :), just to see if things act the same. That would be a start. Aside from that, I can't really comment much, because a lot can happen in your code and I don't see it. Try to create a [minimal and complete, verifiable example](http://stackoverflow.com/help/mcve) which can reproduce the issue and update your question with that. – Whirlwind Dec 18 '16 at 01:05

0 Answers0