Spritekit-在didBecomeActive时保持游戏暂停 [英] Spritekit - Keep the game paused when didBecomeActive

查看:109
本文介绍了Spritekit-在didBecomeActive时保持游戏暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有功能和按钮的暂停系统,它运作完美,我知道当应用程序进入后台时,它会自动暂停,而当它返回时,它会自动取消暂停,我的问题是我不知道如何当它再次变为活动状态时,使其保持暂停状态.

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 ?

推荐答案

在场景或视图中,应该可以通过向其添加观察者来处理暂停

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

现在请记住,我发现在iOS 8中存在一个错误,其中CBApplicationDidBecomeActive可能导致不良结果,因此您需要像这样在SKView的类中覆盖此错误:

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()
    {
    }
}

这篇关于Spritekit-在didBecomeActive时保持游戏暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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