Reputation: 811
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.
Upvotes: 3
Views: 2970
Reputation: 98
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.
Upvotes: 1
Reputation: 1828
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.
Upvotes: 2
Reputation: 496
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 :)
Upvotes: 3