Luiz Luiz - 3 months ago 18
iOS Question

Layer for pause menu

I'm trying to make a pause menu (like this), but if I use the same layer, I can't interact with the scene when it's paused (I guess?!). Everywhere I looked for "pause menu", people said to use another layer. I tried to do it a million times, but it doesn't work (or maybe I'm the one who doesn't know) =(

Thats what I've done so far:

**Declaring**

var gameLayer = SKNode()
var pauseLayer = SKNode()

var gameStarted = Bool()

var Ground = SKSpriteNode()
var Character = SKSpriteNode()

var pauseButton = SKSpriteNode()
var playButton = SKSpriteNode()


**Setup functions**

func setupGround(){

//Ground
Ground = SKSpriteNode(imageNamed: "Ground")
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: 0)
Ground.zPosition = 1

Ground.physicsBody = SKPhysicsBody(rectangleOfSize: Ground.size)
Ground.physicsBody?.categoryBitMask = PhysicsCategory.Ground
Ground.physicsBody?.collisionBitMask = PhysicsCategory.Character
Ground.physicsBody?.contactTestBitMask = PhysicsCategory.Character
Ground.physicsBody?.affectedByGravity = false
Ground.physicsBody?.dynamic = false
}
func setupCharacter(){

//Character
Character = SKSpriteNode(imageNamed: "Character")
Character.size = CGSize (width: 60, height: 40)
Character.position = CGPoint(x: self.frame.width / 2 - Character.frame.width - 100, y: self.frame.height / 2)
Character.zPosition = 2

Character.physicsBody = SKPhysicsBody(circleOfRadius: Character.frame.height / 2.5)
Character.physicsBody?.categoryBitMask = PhysicsCategory.Character
Character.physicsBody?.collisionBitMask = PhysicsCategory.Ground
Character.physicsBody?.contactTestBitMask = PhysicsCategory.Ground
Character.physicsBody?.affectedByGravity = false
Character.physicsBody?.dynamic = true
Character.physicsBody?.allowsRotation = false
}

func setupPauseButton (){

//Pause
pauseButton = SKSpriteNode (imageNamed: "pause")
pauseButton.setScale(0.25)
pauseButton.position = CGPoint(x: self.frame.width / 10, y: self.frame.height / 1.5)
//pauseButton.zPosition = 3
}
func setupPlayButton(){

//Play
playButton = SKSpriteNode (imageNamed: "play")
playButton.setScale(0.15)
playButton.position = CGPoint(x: self.frame.width / 10, y: self.frame.height / 1.5)
//playButton.zPosition = 3
}


**Creating scene**

func createScene(){

self.physicsWorld.contactDelegate = self

setupGround()
gameLayer.addChild(Ground)

setupCharacter()
gameLayer.addChild(Character)

setupPauseButton()
gameLayer.addChild(pauseButton) //add pauseButton to gameLayer
}

func createPauseLayer(){

setupPlayButton()
pauseLayer.addChild(playButton) //add playButton to pauseLayer
}

override func didMoveToView(view: SKView) {
/* Setup your scene here */

self.addChild(gameLayer)

createScene()
createPauseLayer()
}


**Touches began**

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */

//when touch buttons/screen
for touch in touches {

let location = touch.locationInNode(self)
let node = nodeAtPoint(location)

if node == pauseButton{
pauseState()
}

else if node == playButton{
playState()
}

else {

if gameStarted == false{ //game didn't start yet

gameStarted = true

Character.physicsBody?.affectedByGravity = true //start falling when touch the screen

//first jump
Character.physicsBody?.velocity = CGVectorMake(0, 0)
Character.physicsBody?.applyImpulse(CGVectorMake(0, 15))

//first sound effect
//self.runAction(SKAction.playSoundFileNamed("fly.mp3", waitForCompletion: false))
}
else{

//jump
Character.physicsBody?.velocity = CGVectorMake(0, 0)
Character.physicsBody?.applyImpulse(CGVectorMake(0, 15))

//sound effect
//self.runAction(SKAction.playSoundFileNamed("fly.mp3", waitForCompletion: false))
}
}
}
}

//states
func pauseState(){

pauseButton.hidden = true //hide pauseButton
gameLayer.paused = true //pause gameLayer
self.addChild(pauseLayer) //add pauseLayer
}
func playState(){

pauseButton.hidden = false //show pauseButton
pauseLayer.removeFromParent() //remove pauseLayer
gameLayer.paused = false //unpause gameLayer
}

Answer

I would not pause the scene, You should create your game setup like this

SKScene
--GameNode
----All other game related nodes
--OverlayNode
----Your Pause nodes

All the nodes you have besides the pause layer goes into the GameNode All the pause nodes go into the Overlay node

When you are ready to pause your scene, you actually pause your GameNode.

This keeps your scene alive, while your game is paused, so you can use the pause buttons.

When you are playing sounds, play them against nodes inside of the GameNode, not the scene, this way when you pause GameNode, the sounds automatically pause

class GameScene
{
  let pauseLayer = SKNode()
  let gameLayer = SKNode()
  func didMoveToView(view:SKView)
  {
    self.addChild(gameLayer)
  }

  func pauseButton()
  {
    gameLayer.paused = true
    self.physicsWorld.speed = 0;
    self.addChild(pauseLayer)
  }

  func unpauseButton()
  {
    pauseLayer.removeFromParent()
    self.physicsWorld.speed = 1;
    gameLayer.paused = false
  }
}
Comments