M. Turan
M. Turan

Reputation: 21

How to pause and stop SKScene without having performance issues?

I am writing a program using Swift3 in Xcode. I am using SpriteKit and SKScene in my program. As you know, there is a predefined update function that always runs (even when the view changed) in gamescene.sks.

I know that "self.scene.view.paused = YES;" does not work for update functions that are always called.

Is there any way to pause and stop SKScene and its update function?

Upvotes: 0

Views: 921

Answers (1)

crashoverride777
crashoverride777

Reputation: 10674

Apples DemoBot is always a good project to check out.

The best way to pause your game is to create a world node and pause that node instead of pausing the skView.

let worldNode = SKNode()

add it in didMoveToView in your GameScene.

addChild(worldNode)

Than add all the nodes (or SKActions) that you want paused to the worldNode

worldNode.addChild(SOMENODE)
worldNode.addChild(SOMENODE2)

Than in your pause method you say

worldNode.isPaused = true
physicsWorld.speed = 0

and in your resume method you say

worldNode.isPaused = false
physicsWorld.speed = 1

Than for the update method you can exit early if the worlNode is paused

override func update(_ currentTime: TimeInterval) {

     guard !worldNode.isPaused else { return }

     // rest of your code
}

Hope this helps.

Upvotes: 1

Related Questions