Reputation: 41
I am trying to create a pause menu on the game layer. I'm doing this just by adding two buttons on top when the pause button is pressed so the player can still see the stage of the game
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
}
}
}
I have tried sleep() or timer.invalidate() but neither of them work with if statements, and I can't use while loops because then buttons don't appear until the while loop finishes:
while atPoint(location).name != "button_cotinue" {
timer1.invalidate()
}
That is what I tried and doesn't work.
Then when the continue button is pressed I will also delete the buttons but I can code that correctly. As well the finish button sends the player to the main menu scene.
Upvotes: 2
Views: 419
Reputation: 6061
what I do is put the controls (buttons) in a layer named controlsLayer
that is separate from all of the game play items such as player, enemies and obstacles. Then I put all of the game items that I want to be paused in a layer named gameLayer
. I make the controlsLayer
have a high zPosition
like 100.
then when I click my pause button I call gameLayer.isPaused = true
and when I click continue I would call the opposite. gameLayer.isPaused = false
. by just pausing the gameLayer, you can still run other actions like transitions or effects on your pause buttons. If you pause the whole scene you won't be able to run any actions or effects on any items.
Upvotes: 2