3

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.

user3545063
  • 681
  • 8
  • 17

3 Answers3

3

As you said, you cannot pause the scene and keep using it. So you have two choices:

If you want to use the paused property you should add the pause menu, that contains the pause button, as a subview inside your SKView that contains the scene. ie:

-(void)viewWillLayoutSubviews
{
    [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 trigger a method that should un-pause the SKView

Another way is to manage the paused state manually inside the scene using your own pause flag and not updating your game. If you are using a lot of actions this might not be the best solution. The good thing is that you can create the effect of pause not freezing your game which looks cooler :)

gzafra
  • 496
  • 3
  • 10
  • Thanks for the quick response. I am new to programming and I don't really understand how I could access the pause subview from ingame? I mean how can I trigger the viewcontroller from my "gamescene" class? I can't do your second solution btw. since my app uses lots of actions and it would be really difficult to manage everything manually – user3545063 Oct 12 '14 at 11:18
  • You don't actually need to access the ViewController from the scene. All you need is to add a view with the pause button with an action that will pause the skView. Everything happens on the viewcontroller, the scene only receives the pause or un-pause events. Alternatively you can set the ViewController as a delegate of your game scene if you want to add the pause button in the scene itself. – gzafra Oct 12 '14 at 11:52
2

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.

meisenman
  • 1,818
  • 1
  • 15
  • 25
  • Thanks for the response. Could you explain it a bit more? Why does the if check prevent other touch events? And how does the touchesbegan method even work when I set self.pause=YES;? – user3545063 Oct 14 '14 at 20:13
  • `self.pause=YES;` does not prevent touch events. I do those checks so that other game related touches are not fired when interacting with my menu. The menu touches will be above the check so that even if the game is paused you can touch them. I add the check to the update so that it will prevent time from updating. If you have an SKAction and you pause while time is running, when you resume, objects will appear as if the game never paused and kept going the entire time. – meisenman Oct 14 '14 at 20:29
  • Imagine you have an touches event that creates a small sprite where you touched. If the game is paused, you do not want this event to fire and create a new sprite. However, you do want to be able to check if your menu "buttons" were touched. – meisenman Oct 14 '14 at 20:31
1

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