应用程序激活时如何保持 SpriteKit 场景暂停? [英] How to keep SpriteKit scene paused when app becomes active?

查看:26
本文介绍了应用程序激活时如何保持 SpriteKit 场景暂停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法阻止 SpriteKit 在进入前景/变得活跃时自动取消暂停场景?

Is there anyway to prevent SpriteKit from automatically unpausing a scene when entering foreground/becoming active?

我设置 paused = true 并希望它保持不变,即使应用程序在发送到后台后再次变为活动状态.

I set paused = true and want it to remain so even when the app becomes active again after having been sent to the background.

我应该补充一点,我正在迅速执行此操作,尽管我没想到在这方面的行为会有所不同.

I should add that I'm doing this in swift, though I would not have expected the behaviour to be different in this regard.

推荐答案

我对 Knight0fDragon 的解决方案进行了一些调整,使其适合我.这使得 isPaused 将始终等于 realPaused.为了暂停游戏,realPaused"变量只需要改变,isPaused 变量也会自动改变.

I made some adaptions to the solution from Knight0fDragon to make it work for me. This makes it so that isPaused will always be equal to realPaused. In order to pause the game, the "realPaused" variable should then only be altered, which changes automatically also the isPaused variable.

var realPaused: Bool = false {
    didSet {
        self.isPaused = realPaused
    }
}
override var isPaused: Bool {
    didSet {
        if (self.isPaused != self.realPaused) {
            self.isPaused = self.realPaused
        }
    }
}

不幸的是,当应用程序在后台运行时,这将阻止场景暂停.为了防止这种情况,我将条件更改为:self.isPaused != self.realPaused && self.isPaused == false" 这样当应用程序进入后台时场景仍然会自动暂停,但会只有当 realPaused 也为真时才重新激活:

Unfortunately, this will prevent the scene from pausing when the application is running in the background. In order to prevent that, I changed the condition to: "self.isPaused != self.realPaused && self.isPaused == false" so that the scene will still pause automatically when the app is put to the background but will only re-actiate if realPaused is also true:

var realPaused: Bool = false {
    didSet {
        self.isPaused = realPaused
    }
}
override var isPaused: Bool {
    didSet {
        if (self.isPaused == false && self.realPaused == true) {
            self.isPaused = true
        }
    }
}

这篇关于应用程序激活时如何保持 SpriteKit 场景暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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