Reputation: 87
I added a Pause button to my Spritekit Game which pauses the game like this:
@objc func pauseGame()
{
pauseButton?.isHidden = true
pauseMenu?.isHidden = false
gameScene?.isPaused = true // This is the main scene, which is supposed to be paused
}
That works as expected, but the scene still takes input through the touches functions and gesture recognizers and after the scene is continued the character does all kinds of movements, which it isn't supposed to do.
So, basically I just want to know, if there is a best practice to pausing a scene in SpriteKit and if not, which way would be the best to also pause the input while the game is paused.
Upvotes: 0
Views: 81
Reputation: 43
I would advice partial solutions:
Add all game-relevant nodes not directly to the scene, but to an intermediate empty SKNode() (I usually name it "playLayer"). Then you can conveniently pause it with playLayer.isPaused = true. If you need some pause-relevant nodes, you add them to another SKNode "pauseLayer".
To prevent unwanted touch events I add a "pauseBackground" SKSpritenode (which may be almost transparent), overlapping the screen.
A designated bool variable in the scene class (var gameON = true) may also be conveniently used in functions, which are called by the UIGestureRecognizers, to prevent execution of unwanted functions.
Upvotes: 0
Reputation: 6061
pausing the scene doesn't prevent input, as you've already discovered. It only prevents animations, sound and actions from running.
What i do is create a variable for the paused state
var isGamePaused = false
and then when pausing the game set the variable to true
then in the update func and in the touches funcs I put a guard to check if the game is paused
guard !isGamePaused else { return }
that way the update doesn't run and the scene doesn't receive touches. When you want to unPause the scene, just set the isGamePaused = false
when you set the scenes isPaused = false
Upvotes: 0