Reputation: 216
I want to show a "paused" SKLabelNode
when someone clicks on the screen and therefore pauses the sprite kit game.
So I have in touchesBegan
->
[self.pausedLabel setHidden:!self.pausedLabel.hidden];
[self.scene.view setPaused:!self.scene.view.paused];
The Game is paused correctly but the SKLabelNode
is not shown (scene not rendered before paused?!)
If I add a NSTimer
for pausing the scene the label is shown, but then the game continues for that timer-time.
Does anyone have a better solution for this?
Thanks in advance
Upvotes: 2
Views: 1136
Reputation: 130193
I would use SKAction for this. You can use +runBlock: to add the code related to hiding the label, and then use the -runAction method with the completion handler to pause the scene. The runBlock: method may return immediately, but this way, the screen manages to update before the scene is paused.
SKAction *action = [SKAction runBlock:^{
[self.pausedLabel setHidden:!self.pausedLabel.hidden];
}];
[self.pausedLabel runAction:action completion:^{
[self.scene.view setPaused:!self.scene.paused];
}];
Upvotes: 1
Reputation: 13713
Just use a state ivar to determine if the scene should update its content or not.
When you click the button set this state to PAUSE
and in your scene frame update loop test the state.
if (_state != PAUSE) {// Use enum for the state var
// Update scene objects
}
when your button is clicked:
PAUSE
The benefits of this approach is that it allows you to decide exactly what will happen on pause (opposing to pausing the whole scene). You can animate a cool background while in pause or do anything you'd like as this is totally in your control
Upvotes: 0