代码之家  ›  专栏  ›  技术社区  ›  Sam Hoffmann

看法ispaused=true,skscene不工作

  •  2
  • Sam Hoffmann  · 技术社区  · 7 年前

    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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ron Myschuk    7 年前

    问题是,即使场景暂停,代码仍然可以运行。是的,你暂停了这个场景,是的,视觉效果确实在运行。。。但isPaused行之后的视觉效果也会运行并重置您刚刚更改的视觉效果。

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