1

I have a pause system with a function and a button and it works perfect, i know when the app enters background it automatically pauses and when it comes back it automatically unpauses itself, my problem is that I don't know how to keep it paused when it becomes active again.

func applicationWillResignActive(application: UIApplication) {

    NSNotificationCenter.defaultCenter().postNotificationName("Pause", object: nil) // tried here
}        


func applicationDidBecomeActive(application: UIApplication) {

    NSNotificationCenter.defaultCenter().postNotificationName("Pause", object: nil) // tried here
}

I tried on both those methods separately but the game keeps running, sometimes it shows my Pause menu (a layer above the gameplay) and still run in the background. So what is the correct way to achieve this ?

Abdou023
  • 1,654
  • 2
  • 24
  • 45

1 Answers1

0

In your scene or view, you should be able to handle pause by adding an observer to it

    NSNotificationCenter.defaultCenter().addObserver(self,selector:Selector("pauseGame:",name:"Pause",object:nil)

Then you add a function to handle this

func pauseGame(notification:NSNotification)
{
    self.paused = true;
}

Now keep in mind I have found that in iOS 8 there is a bug where CBApplicationDidBecomeActive can cause undesirable results, so you need to override this in your SKView's class like this:

class GameSceneView : SKView
{
    ...//Other Code
    func CBApplicationDidBecomeActive()
    {
    }
}
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • 1
    Of course I have an observer to correspond to the notification, but what is that function and how could it override the one in AppDelegate ?! – Abdou023 Oct 17 '15 at 07:38
  • Appdelegate as a notification that automatically gets sent when did become active is called, so skview calls that function behind the scenes, and messes with the pause, essentially what you are doing is overriding it so that it internal method does not get called – Knight0fDragon Oct 17 '15 at 18:27
  • 1
    What I'm saying is CBDidBecomeActive does not override the function in AppDelegate, it's a custom unrelated one. – Abdou023 Oct 18 '15 at 01:07
  • that doesn't go into AppDelegate it goes into the class that is defined in SKView, it is a private function inside SKview, adding that is a way to cheat the system – Knight0fDragon Oct 18 '15 at 16:13
  • it doesn't override the function call in AppDelegate, iOS has notifications in it that already fire, so you do not need to add another notification, named UIApplicationDidBecomeActiveNotification. This in turn is what will fire the apdelegate's did become active method. SKView also has a listener for this notification, and SKView will call the method CBApplicationDidBecomeActive when it hits. Now in iOS 8, there is a bug that causes it to reset the pause state. so if you place that method into your custom SKView, it won't get called – Knight0fDragon Oct 18 '15 at 16:20
  • is this still a bug in iOS 9 ? – Adam Jan 18 '16 at 22:53
  • No i believe it is fixed in ios 9 – Knight0fDragon Jan 18 '16 at 23:32
  • hmm, well, my game unpauses itself when returning from home screen IF the game was manually paused when i hit home. I've tried everything from every post i can find and nothing is fixing it..! is there an example project somewhere i can look at that shows how to overcome the problem ? – Adam Jan 19 '16 at 08:30
  • Tested it, it is still broken in iOS9, my method fixes it though – Knight0fDragon Jan 19 '16 at 12:56
  • any chance there's an example project somewhere? I can't figure out exactly where to put the class and func ? – Adam Jan 19 '16 at 17:38
  • you put it in the class that is subclassed of skview – Knight0fDragon Jan 19 '16 at 17:39
  • SKView subclassing is not given to you by default, you will need to set it up. You do not do this in the view controller – Knight0fDragon Jan 19 '16 at 17:50