当前位置: 首页 > 面试题库 >

在应用启动/退出时暂停Spritekit游戏。.iOS8

司寇羽
2023-03-14
问题内容

我已经阅读了有关该主题的所有内容,但仍然无法弄清我的问题。我尝试在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)
}

在我的控制器中:

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

func pauseGame(){
    self.skView.paused = true
    self.skView.scene!.paused = true
}

我知道pauseGame可以用,因为如果我在场景中用按钮切换它,它将停止游戏。即使我在将skview和场景加载到控制器中后立即暂停它们,游戏也不会在启动时暂停。在游戏中暂停游戏很容易。但是由于某种原因,每当我退出并恢复该应用程序时,游戏便会自行取消暂停。

我注意到如果我受到黑客攻击并使用某种延迟。.我可以使它工作。但是显然这是非常愚蠢的。我只需要知道游戏在哪里暂停即可!

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

问题答案:

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

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

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)

在应用程序委托中,

5)发布通知以设置应用程序处于活动状态时的停留暂停标志

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

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

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)

在应用程序委托中,

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


 类似资料:
  • 问题内容: 我成功使用以下代码暂停了一个场景游戏: 但是我有一个问题。游戏中的所有随机间隔都会创建对象并将其显示在屏幕上。当我暂停游戏时,它将继续在后台创建对象,而当我继续游戏时,在暂停期间创建的所有对象将同时显示在屏幕上。 我该如何解决? 问题答案: 暂停SKView时,无法将SKLabelNode(或其他任何东西)添加到场景中。您将需要返回运行循环,以便在暂停游戏之前添加您的文本。这是一种方法

  • 问题内容: 我在Swift中创建了一个游戏,其中涉及怪物的出现。怪物根据计时器的出现和消失如下: 然后我就这样称呼它(例如在2秒后生成): 然后,我进行了类似的隐藏操作(在x秒后,我将怪物消灭了)。 因此,我在屏幕顶部创建了一个设置图标,当您点击它时,会出现一个巨大的矩形窗口以更改游戏设置,但是自然的问题是怪物仍然在背景中生成。如果我将玩家带到另一个屏幕,我相信我会失去我所有的游戏状态,并且必须重

  • 这是我的代码: null null 目前,动画似乎暂停悬停,但如果你取消悬停,你可以看到总是突然显示下一个div。所以它并不是真正的“暂停”,它只是停止然后显示下一个div。持续时间丢失。 怎么可能修复呢? 会非常感谢你的帮助!

  • 抱歉我的问题,我被卡住了。我是lib gdx开发游戏的新手,不要严格评判我。我有我的游戏活动: } 我有两个屏幕: 还有我有暂停按钮的游戏屏幕: 还有我的主要问题--

  • 我有一个运行播放web应用程序的docker映像。在dockerfile中有一个CMD,它启动服务器,并等待您按下Ctrl+D退出。如果我这样做: 它工作正常-启动服务器并等待Ctrl+D。 然而,当我启动容器时,情况并非如此: 如何强制docker start不停止服务器?

  • 每当我打开我的android应用程序,它就会触发三星galaxy S8上的游戏模式/游戏启动器。但这个应用程序不是一个游戏。在以前的版本中,该应用程序使用opengl,如果使用opengl,三星似乎将应用程序视为游戏。我已经删除了opengl代码,但三星仍然将该应用程序视为游戏。奇怪的是,当我更改应用程序ID时,应用程序会正常启动。所以,我认为三星将application-id作为游戏存储在某个地