Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 11: Pausing - Home Button #3

Open
MarkThielen opened this issue Oct 20, 2016 · 4 comments
Open

Chapter 11: Pausing - Home Button #3

MarkThielen opened this issue Oct 20, 2016 · 4 comments

Comments

@MarkThielen
Copy link

Hey there,

the pause within the game tapping the pause/resume button works without problems, but if you pause and hit the Home button, return to the game the game will not resume but only the background seems to be resumed while player, meteors and stars are frozen so to say.

Cheers,
Mark

@spritekitbook
Copy link
Owner

Hi Mark,

The idea is that we are registering for notifications in GameScene called "Pause" and "Resume" in the didMove(to:) method.

        // Register notifications for "Pause"
        NotificationCenter.default.addObserver(self, selector: #selector(statePaused), name: NSNotification.Name(rawValue: "Pause"), object: nil)

        // Register notifications for "Resume"
         NotificationCenter.default.addObserver(self, selector: #selector(resumeGame), name: NSNotification.Name(rawValue: "Resume"), object: nil)

The chain of events that happen when the game comes back into the foreground to resume the game go like this.

  1. AppDelegate runs the applicationDidBecomeActive(application:) method which runs this code.
    func applicationDidBecomeActive(_ application: UIApplication) {
        // Resume the view
        let view = self.window?.rootViewController?.view as! SKView
        view.isPaused = false

        // Post message to Resume the game
        NotificationCenter.default.post(name: Notification.Name(rawValue: "Resume"), object: nil)
    }
  1. GameScene receives a "Resume" notification to call the resumeGame() method.

  2. The resumeGame() method fires a timer that delays for one second before calling the stateResume() method.

    func resumeGame() {
        // Run a timer that resumes the game after 1 second
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(stateResume), userInfo: nil, repeats: false)
    }
  1. Finally, the stateResume() method restores the state from the previous state and puts the speed of the gameNode back to the default, 1.0
    func stateResume() {
        state = previousState

        gameNode.speed = 1.0

        if kDebug {
            print("Resuming")
        }
    }

Let me know if that helps you out.

Jeremy

@MarkThielen
Copy link
Author

Thanks Jeremy,

I did do the following :

  • load xcode project from chapter 14 finish
  • compiled and run in simulator
  • hit "Play" on first screen
  • hit "Start" on Game Scene
  • Game starts, wait for meteors and stuff to come down
  • hit "Pause" button upper right corner
  • hit "Home" Button (Shift+Cmd+H) exiting game
  • open SpaceRunner Icon to go back to game
  • See screen that I left with Background animated and Music off (nothing else moves)
  • Hit "Play" Button in upper right corner, Music starts, nothing else moves

I assume that the expected behavior would be that the Game just resumes as if you would just hit pause/play within the game ... but it does not. I would also agree that the speed=1.0 should start everything.

Will have a look into it, maybe I can find it or it is just something local for me.

Cheers,
Mark

@spritekitbook
Copy link
Owner

Okay, thank you for detailing your steps.

If I understand correctly you are pausing, and then hitting the home button. The game is not resuming after bringing it back into the foreground and pressing the resume button. Correct?

Yes this is a bug. It is written to be mutually exclusive, either pressing the home button or pressing the pause button.

I will address this tonight when I get home and make an update. Thank you for pointing it out.

Jeremy

@MarkThielen
Copy link
Author

Yeah, that is exactly it. Looking through the code I thought that while the Pause vs. the Home Button is a bit different, logically it should work the same.

-Mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants