Pause the game and display the menu in SpriteKit

I would like to implement a pause menu in my SpriteKit game and I always find the code

self.scene.view.paused == YES


however, it stops everything inside the game, even the touchBegan method. So when I show the menu before I pause the game, I cannot interact with it as touchhesBegan is not working. I was looking for this issue and some people just added the menu as SKNode, but that still doesn't help as it ignores touch inputs when paused.


source to share

3 answers

When you press the pause button, set self.pause=YES;

Bring your "pause menu touch" before your touch event starts. Immediately after these checks add:

if(self.pause == YES)


This will prevent other touch events from firing.

Add this line to the beginning of your update method to stop time from updating while you should be paused.

Here's how to essentially freeze time and physics while still being able to tap on pause menu items.



As you said, you cannot pause a scene and keep using it. Thus, you have two options:

If you want to use the paused property, you must add a pause menu that contains a pause button as a peek inside yours SKView

that contains the scene. i.e:

    [super viewWillLayoutSubviews];

    SKView * skView = (SKView *)self.view;
    UIView *pauseMenu = [[UIView alloc] initWithFrame:rect]; // This should be the pause menu with the pause button

    [skview addSubview:pauseMenu];


The pause button will call the method that should cancel the pause SKView

Another way is to manage the paused state manually within the scene using your own pause flag, rather than refreshing your game. If you are using a lot of actions, this may not be the best solution. It's good that you can create a pause effect and not freeze your game, which looks cooler :)



In my application, I also found that self.scene.view.paused == YES pauses everything including touch and animation events. However, I also found that self.scene.paused == YES will pause all scene nodes and their actions / animations, but will NOT affect touch events such as any nodes / buttons in the pause menu. I believe pausing the view will affect touch events, but pausing the scene will not.



All Articles