在Spritekit中创建暂停菜单

时间:2018-05-08 02:26:27

标签: ios swift sprite-kit swift4

我正在尝试在游戏层上创建暂停菜单。我只是通过在按下暂停按钮时在顶部添加两个按钮来做到这一点,这样玩家仍然可以看到游戏的阶段

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)

        if atPoint(location).name == "pause" {
            let button: SKSpriteNode?
            let button2: SKSpriteNode?

            button = SKSpriteNode(imageNamed: "button_continue")
            button?.name = "button_continue"
            button?.size.height = 55
            button?.size.width = 215
            button?.physicsBody = SKPhysicsBody(rectangleOf: button!.size)
            button?.physicsBody?.allowsRotation = false
            button?.position = CGPoint(x: 0, y: 160)
            button?.zPosition = 100
            self.addChild(button!)

            button2 = SKSpriteNode(imageNamed: "button_finish")
            button2?.name = "button_finish"
            button2?.size.height = 55
            button2?.size.width = 215
            button2?.physicsBody = SKPhysicsBody(rectangleOf: button2!.size)
            button2?.physicsBody?.allowsRotation = false
            button2?.position = CGPoint(x: 0, y: -50)
            button2?.zPosition = 100
            self.addChild(button2!)

            // The two buttons are created but here I am trying to stop everything until the continue button is pressed and I don't find a way
        } 
    }
}

我已经尝试过sleep()或者timer.invalidate(),但是它们都不能用于if语句,而且我不能使用while循环,因为在while循环结束之前按钮不会出现:< / p>

while atPoint(location).name != "button_cotinue" {
    timer1.invalidate()
}

这就是我尝试过但不起作用的。

然后当按下继续按钮时,我也会删除按钮,但我可以正确编码。完成按钮也会将播放器发送到主菜单场景。

1 个答案:

答案 0 :(得分:2)

我所做的是将控件(按钮)放在名为controlsLayer的图层中,该图层与玩家,敌人和障碍物等所有游戏项目分开。然后我将所有想要暂停的游戏项目放在名为gameLayer的图层中。我让controlsLayer有一个高zPosition,如100。

然后当我点击暂停按钮时,我拨打gameLayer.isPaused = true,当我点击“继续”时,我会拨打相反的电话。 gameLayer.isPaused = false。通过暂停gameLayer,你仍然可以在暂停按钮上运行其他操作,如过渡或效果。如果您暂停整个场景,您将无法对任何项目运行任何操作或效果。