Pranav Sharan
Pranav Sharan

Reputation: 37

How do I create a pause and unpause button in Swift and Sprite Kit?

I have the following code. When the program runs, the game pauses when you click the pause button in the top left, but the unpause button in the middle does not show. It seems like the program is running the command to pause the game before it is running the command to create the unpause button but I don't know how to fix this.

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var lanecounter:CGFloat = 0
var lastOpen:CGFloat = 0
var score:Int = 0
var BGspeed:CGFloat = -700
var player:SKSpriteNode?
let scoreLabel = SKLabelNode()
let highScoreLabel = SKLabelNode()
var direction:Int?
let noCategory:UInt32 = 0
let carCategory:UInt32 = 0b1
let playerCategory:UInt32 = 0b1 << 1
let pointCategory:UInt32 = 0b1 << 2
let bumperCategory:UInt32 = 0b1 << 3
var died:Bool?
var pause:Bool = false
var gameStarted:Bool?
var restartBTN = SKSpriteNode()
var pauseBTN = SKSpriteNode()
var unpauseBTN = SKSpriteNode()

func createPauseBTN()
{
    pauseBTN = SKSpriteNode(color: SKColor.purple, size: CGSize(width: 100, height: 100))
    pauseBTN.position = CGPoint(x: -self.frame.width/2 + 20, y: self.frame.height/2 - 20)
    pauseBTN.zPosition = 10
    self.addChild(pauseBTN)
}

func createunPauseBTN()
{
    unpauseBTN = SKSpriteNode(color: SKColor.purple, size: CGSize(width: 100, height: 100))
    unpauseBTN.position = CGPoint(x: 0, y: 0)
    unpauseBTN.zPosition = 1000
    self.addChild(unpauseBTN)
    //pauseGame()
    //scene?.view?.isPaused = true
}

func pauseGame() {
    scene?.view?.isPaused = true
    createunPauseBTN()
}    



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches
    {
        let location = touch.location(in: self)

        if died == true
        {
            if restartBTN.contains(location) {
            restartScene()
            }
        }

        if pauseBTN.contains(location) {
            print(pause)
            createunPauseBTN()
            print("asd")
            pauseBTN.removeFromParent()
            pauseGame()
        }

        if unpauseBTN.contains(location) {
            scene?.view?.isPaused = false
            createPauseBTN()
        }
    }
}

Upvotes: 0

Views: 2253

Answers (1)

crashoverride777
crashoverride777

Reputation: 10664

Your problem is that you are pausing the SKView which will pause everything.

 scene?.view?.isPaused = true

That means everything in your SKScene such as SKActions, addingChildren etc will not work/stop working until you set it to false again.

It is usually a better idea to create a worldNode and add all nodes that you need paused to that node. Apple also does this in DemoBots.

So add another property in your class

let worldNode = SKNode()

and add it to the scene in DidMoveToView

addChild(worldNode)

Than add all your other nodes that you need paused such as your player, enemies etc to this worldNode

 worldNode.addChild(someNode1) 
 worldNode.addChild(someNode2) 

Than in your pause code you should say this

 worldNode.isPaused = true
 physicsWorld.speed = 0

and in the resume code this

 worldNode.isPaused = false
 physicsWorld.speed = 1

This should give you more flexibility because you can still do things such as letting certain SKActions run e.g for your background. It should also make your pause action smoother.

Hope this helps

Upvotes: 3

Related Questions