在应用程序启动/退出时暂停spritekit游戏.. iOS8

ham*_*obi 10 nsnotificationcenter ios sprite-kit swift ios8

我已经阅读了关于这个主题的所有内容,但仍然无法弄清楚我的问题.我试过在appdelegate的每个区域暂停我的游戏

func applicationWillResignActive(application: UIApplication!) {
    NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}

func applicationDidEnterBackground(application: UIApplication!) {
    NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}

func applicationWillEnterForeground(application: UIApplication!) {
    NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}

func applicationDidBecomeActive(application: UIApplication!) {
    NSNotificationCenter.defaultCenter().postNotificationName("pauseGameScene", object: self)
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

override func viewDidLoad() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGame:", name: "pauseGameScene", object: nil)
}

func pauseGame(){
    self.skView.paused = true
    self.skView.scene!.paused = true
}
Run Code Online (Sandbox Code Playgroud)

我知道pauseGame有效,因为如果我用场景中的按钮切换它,它将停止游戏.即使我将skview和场景加载到控制器后直接暂停我的场景和场景......游戏也不会在启动时暂停.当我在游戏中时,很容易暂停游戏.但出于某种原因,每当我退出并恢复应用程序时,游戏将自行暂停.

我注意到,如果我得到hacky并使用某种延迟..我可以让它工作.但显然这是非常愚蠢的......我只需要知道游戏本身在哪里取消!

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

func pauseGame(sender: UIButton!){

    delay(2) {
        println("blah")
        self.skView.paused = true
        self.skView.scene!.paused = true
    }
}
Run Code Online (Sandbox Code Playgroud)

0x1*_*41E 7

这是从后台模式返回后保持视图暂停的方法.

Xcode 7(参见下面的Xcode 8说明)

在故事板中,

1)将视图的类更改为MyView

在视图控制器中,

2)使用名为stayPaused的布尔值定义SKView子类

class MyView: SKView {
    var stayPaused = false

    override var paused: Bool {
        get {
            return super.paused
        }
        set {
            if (!stayPaused) {
                super.paused = newValue
            }
            stayPaused = false
        }
    }

    func setStayPaused() {
        if (super.paused) {
            self.stayPaused = true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

3)将视图定义为MyView

4)添加通知程序以设置stayPaused标志

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
            // Configure the view.
            let skView = self.view as MyView

            NSNotificationCenter.defaultCenter().addObserver(skView, selector:Selector("setStayPaused"), name: "stayPausedNotification", object: nil)
Run Code Online (Sandbox Code Playgroud)

在App Delegate中,

5)在应用程序变为活动状态时发布通知以设置停留暂停标志

func applicationDidBecomeActive(application: UIApplication) {
    NSNotificationCenter.defaultCenter().postNotificationName("stayPausedNotification", object:nil)
}
Run Code Online (Sandbox Code Playgroud)

Xcode 8

在故事板中,

1)将视图的类更改SKViewMyView

在视图控制器中,

2)SKView使用名为stayPaused的布尔值定义子类

class MyView: SKView {
    var stayPaused = false

    override var isPaused: Bool {
        get {
            return super.isPaused
        }
        set {
            if (!stayPaused) {
                super.isPaused = newValue
            }
            stayPaused = false
        }
    }

    func setStayPaused() {
        if (super.isPaused) {
            self.stayPaused = true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

3)将视图定义为MyView

4)添加通知程序以设置stayPaused标志

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let view = self.view as! MyView? {
            NotificationCenter.default.addObserver(view, selector:#selector(MyView.setStayPaused), name: NSNotification.Name(rawValue: "stayPausedNotification"), object: nil)
Run Code Online (Sandbox Code Playgroud)

在App Delegate中,

5)在应用程序变为活动状态时发布通知以设置停留暂停标志

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "stayPausedNotification"), object: nil)
}
Run Code Online (Sandbox Code Playgroud)

  • 我们必须将skview子类化以使某些应该已经有效的工作变得很糟糕.但是,嘿,至少它有效 (2认同)