Squid
Squid

Reputation: 315

What is the best way to create a pause menu?

I want to make a menu that gets activated by tapping a SKSpriteNode, then pauses everything in my game except for the resume and restart button and also gets activated when the user leaves the app.

I'm currently doing this by having a bool call isPaused and all the methods only get activated when isPaused is set to NO. Whenever the pause node is touched it gets set to YES and everything stops. This, however, causes me a lot of trouble, for instance, when didEnterBackground is called I make that call the method that pauses the scene, but when you reopen the app the pause menu is not there.

I want to know what the best way of doing this is.

Upvotes: 3

Views: 760

Answers (3)

Christian
Christian

Reputation: 22343

I'd recommend to use a 'game-layer' node. It's like a super-node which contains all the nodes from your game.

The trick is, that if you then call gameLayer.paused = true, all sub-nodes also will pause, because the pause function works recursively.

for example:

Swift:

var gameLayer = SKNode()

var node1 = SKSpriteNode()
var node2 = SKSpriteNode()

var specialNode = SKSpriteNode()

gameLayer.addChild(node1)
gameLayer.addChild(node2)

gameLayer.pause = true

//node1 and node2 will pause also

Objective-C

SKSpriteNode *node1 = [SKSpriteNode new];
SKSpriteNode *node2 = [SKSpriteNode new];
SKSpriteNode *specialNode = [SKSpriteNode new];

[gameLayer addChild: node1];
[gameLayer addChild: node2];

gameLayer.paused = TRUE;

Now you can call gameLayer.paused = true where ever you want and pause all the nodes at once.

Upvotes: 3

Siriss
Siriss

Reputation: 3767

Check out my question here: didBecomeActive un-pauses game

I can see if I can post my code (under NDA) when I get to my computer. The answer does work, and my comment about adding a timer works well also.

Upvotes: 0

sangony
sangony

Reputation: 11696

To continue your train of thought, use the BOOL in the update method like this:

-(void)update:(CFTimeInterval)currentTime {
    if(isPaused) {
        // handle only processing during a pause
    } else {
        // handle processing while not paused
    }
}

Upvotes: 1

Related Questions