SpriteKit-didBecomeActive时不显示暂停屏幕 [英] SpriteKit - Pause screen doesn't show when didBecomeActive

查看:79
本文介绍了SpriteKit-didBecomeActive时不显示暂停屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的暂停系统可以在游戏内部完美运行,并且当应用程序移至后台然后再次变为活动状态时,游戏仍保持暂停状态,但是我现在的问题是当它变为活动状态时,我的暂停屏幕无法显示.

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:

AppDelegate:

func applicationDidBecomeActive(application: UIApplication) {

    NSNotificationCenter.defaultCenter().postNotificationName("Pause", object: nil)

}

ViewController:

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):

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:

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
        }
}

推荐答案

设置暂停屏幕和按钮的hidden属性无效,因为视图和/或场景已暂停.您将需要暂时取消暂停视图,将隐藏属性设置为false,返回主循环,然后重新暂停视图.这是如何执行此操作的示例.

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:

AppDelegate:

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

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

MainVew(SKView子类):

MainVew (SKView subclass):

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:

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:

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
}

您可以通过以下方式在暂停状态之间进行切换

You can toggle between the pause states with

if let customView = self.view as? MyView {
    customView.togglePause()
}

这篇关于SpriteKit-didBecomeActive时不显示暂停屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆