查看?.ispaused = true,skscene无效

时间:2017-10-04 13:24:10

标签: 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

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

1 个答案:

答案 0 :(得分:1)

问题是,即使场景已暂停,代码仍会运行。是的,你正在暂停场景,是的,视觉效果确实会运行......但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
            }
        }
    }
}