Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Swift view?.ispaused=在skscene不工作的情况下为真_Swift_Swift3_Sprite Kit_Skaction_Skscene - Fatal编程技术网

Swift view?.ispaused=在skscene不工作的情况下为真

Swift view?.ispaused=在skscene不工作的情况下为真,swift,swift3,sprite-kit,skaction,skscene,Swift,Swift3,Sprite Kit,Skaction,Skscene,因此,我正在使用SpriteKit中的一个游戏,并使用此代码暂停游戏 self.pauseButton.alpha = 0 self.playButton.alpha = 1 self.settingsBackground.alpha = 0.85 self.isPaused = true self.pauseButton.alpha = 1 self.playButton.alpha = 0 self.settingsBackground.alpha = 0 暂停前运行的代码

因此,我正在使用
SpriteKit
中的一个游戏,并使用此代码暂停游戏

self.pauseButton.alpha = 0
self.playButton.alpha = 1
self.settingsBackground.alpha = 0.85    
self.isPaused = true    
self.pauseButton.alpha = 1
self.playButton.alpha = 0
self.settingsBackground.alpha = 0

暂停前运行的代码用于更改暂停的外观,然后代码将恢复暂停。问题是暂停前的代码并没有运行,相反,游戏只是在改变视觉效果之前暂停。我尝试过添加延迟,使步骤
SKActions
,并在暂停之前测试代码。当我只运行前3行时,视觉效果会发生适当的变化,但很明显,游戏不会暂停。当我运行整个过程时,游戏会暂停,但视觉效果不会改变。救命啊

问题是,即使在场景出现时,代码仍会运行。是的,您正在暂停场景,是的,视觉效果确实得到了运行…但isPaused行之后的视觉效果也得到了运行,并重置了您刚刚更改的视觉效果

这是一个非常简单的例子,说明了它是如何工作的。现场有3个盒子;底部框有一个重复上下缩放的动作,并在场景暂停时停止。按下顶部框将暂停游戏,并显示将取消游戏暂停的中间框

class GameScene: SKScene {

    private var test: SKSpriteNode!
    private var test2: SKSpriteNode!
    private var test3: SKSpriteNode!

    override func didMove(to view: SKView) {

        backgroundColor = .clear

        test = SKSpriteNode(color: .blue, size: CGSize(width: 200, height: 200))
        test.position = CGPoint(x: 0, y: 350)
        addChild(test)

        test2 = SKSpriteNode(color: .red, size: CGSize(width: 200, height: 200))
        test2.position = CGPoint(x: 0, y: 0)
        test2.alpha = 0
        addChild(test2)

        test3 = SKSpriteNode(color: .yellow, size: CGSize(width: 200, height: 200))
        test3.position = CGPoint(x: 0, y: -350)
        addChild(test3)

        let scaleDown = SKAction.scale(to: 0.1, duration: 1.0)
        let scaleUp = SKAction.scale(to: 1.0, duration: 1.0)
        let sequence = SKAction.sequence([scaleDown, scaleUp])
        let repeater = SKAction.repeatForever(sequence)
        test3.run(repeater)
    }

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

        if let touch = touches.first as UITouch! {

            let touchLocation = touch.location(in: self)

            if test.contains(touchLocation) {
                print("pausing")
                self.test2.alpha = 1
                self.test.alpha = 0
                self.isPaused = true
            }

            if test2.contains(touchLocation) {
                print("unpausing")
                self.test2.alpha = 0
                self.test.alpha = 1
                self.isPaused = false
            }
        }
    }
}
class游戏场景:SKScene{
私人var测试:SKSpriteNode!
私有var test2:SKSpriteNode!
私有变量test3:SKSpriteNode!
覆盖func didMove(到视图:SKView){
背景颜色=.clear
测试=SKSpriteNode(颜色:蓝色,尺寸:CGSize(宽度:200,高度:200))
test.position=CGPoint(x:0,y:350)
addChild(测试)
test2=SKSpriteNode(颜色:红色,尺寸:CGSize(宽度:200,高度:200))
test2.position=CGPoint(x:0,y:0)
test2.alpha=0
addChild(test2)
test3=SKSpriteNode(颜色:黄色,尺寸:CGSize(宽度:200,高度:200))
test3.position=CGPoint(x:0,y:-350)
addChild(test3)
设scaleDown=SKAction.scale(到:0.1,持续时间:1.0)
让scaleUp=SKAction.scale(到:1.0,持续时间:1.0)
让sequence=SKAction.sequence([scaleDown,scaleUp])
让repeater=SKAction.repeatForever(序列)
test3.run(中继器)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
如果让触摸=触摸。首先作为UITouch{
让touchLocation=touch.location(in:self)
如果测试包含(触摸位置){
打印(“暂停”)
self.test2.alpha=1
self.test.alpha=0
self.isPaused=true
}
如果test2.contains(触摸位置){
打印(“取消打印”)
self.test2.alpha=0
self.test.alpha=1
self.isPaused=false
}
}
}
}

这很有道理。谢谢我完全不认为这会发生,因为动作和图形都暂停了。我想我会通过将还原代码放在isPaused=false所在的位置来修复它。很好,很高兴它帮助您解决了这个问题