gpt4 book ai didi

ios - 在 Spritekit 中创建暂停菜单

转载 作者:搜寻专家 更新时间:2023-11-01 07:02:37 25 4
gpt4 key购买 nike

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

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 循环完成后按钮才会出现:

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

这是我尝试过的方法,但没有用。

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

最佳答案

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

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

关于ios - 在 Spritekit 中创建暂停菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50224891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com