Hello! There is a function that pauses the game.

func pauseTheGame() { self.scene?.isPaused = true //так же здесь прописано несколько других параметров, типа появление надписи, кнопок и т.д. } 

This function is called in two cases.

1) When the player presses the pause button during the game.

2) When the application is sent to the background.

But there is one small problem with notifications. A small code example:

From AppDelegate

 func applicationWillResignActive(_ application: UIApplication) { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "goToBackground"), object: self) } 

From the game scene:

 NotificationCenter.default.addObserver(self, selector: #selector(GameScene.pauseTheGame), name: NSNotification.Name("goToBackground"), object: nil) 
  1. The first problem is that when the application is minimized, the game is paused, i.e. function is called and everything goes on as usual. But when the application becomes active again, the game itself turns off the pause and the game continues. That is, the value of the pause somehow changes itself to the value "false". While the rest of things, such as buttons and text, remain in place. Tell me what could be the problem?
  2. As I said above. The pause function is called either when the button is pressed or when it is minimized. I would like to know how to make the function not called twice. That is, if the player paused, and after that he also turned off the application. That is, the function is called twice. And you have to press the button "PLAY" twice.
  • one
    show what you have in pauseTheGame - Max Mikheyenko
  • Try to write more detailed questions. Explain exactly what you see the problem, how to reproduce it, what you want to get as a result, etc. - Nicolas Chabanovsky
  • magic is shorter. I understand the project is not yours and where there are pieces of code that you do not know about. In particular, two pauses followed by a double press of play is someone who has puzzled you up. if you want to lay out the whole site, we’ll think something - Max Mikheyenko

1 answer 1

As I understand it, you have the same function and pauses and continues the game. The solutions here are like fantasy.

  1. You can break into different functions pause and start the game.
  2. You can break into different functions call pause the user and call pause care in the background.
  3. You can add a flag that pauses the user and use it
  4. You can add a flag that pauses to go to the background and use it.
  5. Use p.3 + p.4
  • No, you did not understand correctly. I am afraid that you did not carefully read the question. - Nexus S.