Search code examples
iosobjective-csprite-kitpausing-execution

Pausing a game and displaying a menu in SpriteKit


I'd like to implement a pause menu to my SpriteKit game and I always come across the code

self.scene.view.paused == YES

however it pauses everything inside the game, even the touchesBegan method. So when I display a menu before I pause the game, I can't interact with it since touchesBegan doesn't work. I've googled this problem and some people just say to add the menu as an SKNode, but that still doesn't help since it ignores touch inputs in the pause state.


Solution

  • When you click your pause button, set self.pause=YES;

    Bring your "pause menu touch checks" to the beginning of your touch event. Directly after these checks, add:

    if(self.pause == YES)
         return;
    

    This will prevent other touch events from firing.

    Add this line to the very beginning of your update method as well to stop time from updating while you are supposed to be paused.

    This is how to essentially freeze time and physics while still being able to touch pause menu items.