SpriteKit - 当didBecomeActive时,暂停屏幕不会显示

时间:2015-10-25 07:21:09

标签: sprite-kit appdelegate pause

我的暂停系统在游戏内完美运行,当应用程序移动到后台然后再次激活时游戏暂停,但我现在的问题是当它变为活动时我的暂停屏幕没有显示。

的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(我的自定义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
        }
}

1 个答案:

答案 0 :(得分:1)

设置暂停屏幕和按钮的隐藏属性无效,因为视图和/或场景已暂停。您需要暂时取消暂停视图,将隐藏属性设置为false,返回主循环,然后重新暂停视图。这是一个如何做到这一点的例子。

的AppDelegate:

func applicationWillResignActive(application: UIApplication) {        
    NSNotificationCenter.defaultCenter().postNotificationName("PauseViewNotification", object:nil)
}

func applicationDidBecomeActive(application: UIApplication) {
    NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseScreenNotification", object:nil)
}

MainVew(SKView子类):

class MainView: SKView {
    override var paused: Bool {
        get {
            return super.paused
        }
        set {

        }
    }

    func pause() {
        super.paused = true
    }

    func resume() {
        super.paused = false
    }

    func togglePause() {
        super.paused = !super.paused
    }
}

的ViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene(fileNamed:"GameScene") {

        // Configure the view.
        let skView = self.view as! MainView

        NSNotificationCenter.defaultCenter().addObserver(skView, selector:Selector("pause"), name: "PauseViewNotification", object: nil)
    }
}

GameScene:

override func didMoveToView(view: SKView) {
    NSNotificationCenter.defaultCenter().addObserver(self, selector:Selector("pauseGame"), name: "ShowPauseScreenNotification", object: nil)
}

func pauseGame() {
    if (!isFirstTime) {
        pauseScreen.hidden = false
        pauseButton.hidden = false
        // Un-pause the view so the screen and button appear
        if let customView = self.view as? MainView {
            customView.resume()
        }
        // Re-pause the view after returning to the main loop
        let pauseAction = SKAction.runBlock({
            [weak self] in
            if let customView = self?.view as? MainView {
                customView.pause()
            }
        })
        runAction(pauseAction)
    }
    isFirstTime = false
}

您可以使用

在暂停状态之间切换
if let customView = self.view as? MyView {
    customView.togglePause()
}