My pause system works perfectly from inside the game, and also when the app moves to the background and then becomes active again the game stays paused, but my problem now is when it becomes active my pause screen doesn't show.
AppDelegate:
func applicationDidBecomeActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("Pause", object: nil)
}
ViewController:
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene()
// Configure the view.
let skView = self.view as! MainView
NSNotificationCenter.defaultCenter().addObserver(skView, selector: "setStayPaused", name: "Pause", object: nil)
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
MainView (My custom skView):
class MainView: SKView {
var stayPaused = false as Bool
override var paused: Bool {
get {
return super.paused
}
set {
if (!stayPaused) {
super.paused = newValue
}
stayPaused = false
}
}
func setStayPaused() {
if (super.paused) {
self.stayPaused = true
}
}
}
GameScene:
override func didMoveToView(view: SKView) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGame", name: "Pause", object: nil)
}
func pauseGame() {
if isFirstTime == false { // to make sure that the app did not just get launched
pauseScreen.hidden = false // doesn't show
pauseButton.hidden = false // doesn't show
view?.paused = true
scene?.paused = true
}
}
Setting the pause screen's and button's hidden property has no effect because the view and/or scene is paused. You will need to temporarily un-pause the view, set the hidden properties to false, return the main loop, and then re-pause the view. Here's an example of how to do that.
AppDelegate:
MainVew (SKView subclass):
ViewController:
GameScene:
You can toggle between the pause states with