Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 暂停层在从后台启动应用程序后取消暂停_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios 暂停层在从后台启动应用程序后取消暂停

Ios 暂停层在从后台启动应用程序后取消暂停,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我已将“播放/暂停”按钮设置为gameLayer-此处: 一切正常,但我对后台的应用程序无能为力。当我暂停游戏层,应用程序转到后台时,或者当我锁定设备时,应用程序转到前台后,游戏层将自动取消暂停。如何预防 谢谢 这就是我的工作原理: import SpriteKit import GameplayKit class GameScene: SKScene { var gameLayer = SKNode() override func didMove(to view: SKV

我已将“播放/暂停”按钮设置为gameLayer-此处:

一切正常,但我对后台的应用程序无能为力。当我暂停游戏层,应用程序转到后台时,或者当我锁定设备时,应用程序转到前台后,游戏层将自动取消暂停。如何预防


谢谢

这就是我的工作原理:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    var gameLayer = SKNode()

    override func didMove(to view: SKView) {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(GameScene.pause),
                                               name: .UIApplicationDidBecomeActive,
                                               object: nil)
        let moveUp = SKAction.moveBy(x: 0, y: 100, duration: 2)
        let sequence = SKAction.sequence([moveUp, moveUp.reversed()])
        let loop = SKAction.repeatForever(sequence)

        let sprite = SKSpriteNode(color: .purple, size: CGSize(width: 100, height: 100))

        addChild(gameLayer)
        gameLayer.addChild(sprite)

        sprite.run(loop)
    }

    func pause(){

        gameLayer.speed = 0
    }

    override func willMove(from view: SKView) {
        super.willMove(from: view)

        NotificationCenter.default.removeObserver(self,
                                                  name: .UIApplicationDidBecomeActive,
                                                  object: nil)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        gameLayer.speed = gameLayer.speed == 0 ? 1 : 0
    }
}
导入SpriteKit
导入游戏工具包
类游戏场景:SKScene{
var gameLayer=SKNode()
覆盖func didMove(到视图:SKView){
NotificationCenter.default.addObserver(self,
选择器:#选择器(游戏场景.暂停),
名称:.UIApplicationIDBecMeactive,
对象:无)
让moveUp=SKAction.moveBy(x:0,y:100,持续时间:2)
让sequence=SKAction.sequence([moveUp,moveUp.reversed()]))
让循环=SKAction.repeatForever(序列)
设sprite=SKSpriteNode(颜色:紫色,尺寸:CGSize(宽度:100,高度:100))
addChild(游戏玩家)
gameLayer.addChild(精灵)
sprite.run(循环)
}
函数暂停(){
gameLayer.speed=0
}
覆盖func willMove(从视图:SKView){
super.willMove(从:视图)
NotificationCenter.default.removeObserver(self,
名称:.UIApplicationIDBecMeactive,
对象:无)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
gameLayer.speed=gameLayer.speed==0?1:0
}
}
我没有时间做更多的测试,但我希望这样:

 func pause(){
        gameLayer.isPaused = true
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       gameLayer.isPaused = !gameLayer.isPaused
    }
func暂停(){
gameLayer.isPaused=true
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
gameLayer.isPaused=!gameLayer.isPaused
}

将工作,但令人惊讶的是它没有工作(在执行
pause()
方法后,节点继续移动)。我真的不知道这是一个bug还是一个特性:)但是我想说,这是一个bug。无论如何,您可以使用speed属性实现您想要的。只需复制并粘贴此代码即可查看其工作原理。

只需在场景中侦听UIApplicationIDBecMeactivateNotification并采取适当的操作(暂停游戏层)。如果您仍然被卡住,请告诉我,我会给您写一个答案。我查看了苹果的文档,发现只有
static let-uiapplicationdibecomeactive:NSNotification.Name
,但我不知道如何准确地使用它。榜样将是伟大的!谢谢也许像@Knight0fDragon或@AlessandroOrnano这样的人可以帮助解释为什么在这种情况下,
isPaused
不起作用。。。也许我只是因为简短的测试而遗漏了一些东西,但它看起来像一个bug。还有一个旁注。。。由于
uiapplicationdibecomeactive
通知在应用程序的最开始启动时启动,
gameLayer
将从一开始就暂停。因此,您必须点击屏幕取消使精灵上下移动的动作的暂停。
isPaused
行肯定有效。我复制了您的代码,它按预期工作。但奇怪的是,这些通知不会fire@RonMyschuk嗨,罗恩,谢谢你看这个
isPaused
touchsbegind
中也适用于我。此外,通知被正确触发。但是,正如我提到的,不知何故,在
pause()
方法中更改
isPaused
属性没有效果。请在
pause()
方法中设置断点,确认从后台返回时未执行断点,好吗?我已经尝试过了,但什么都没有。我以前从未使用过此特定通知,过去也很少使用这些通知。也许我把它设置错了。我像在代码中一样设置了它,但是我需要在AppDelegate中这样做吗?我也很好奇这里发生了什么