This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.
Based on Gnome's recent A Simple Pause menu, here's a more extended tutorial.
There are quite a number of things you shouldn't forget, as we will learn below!
Using a drawing program like Photoshop, Gimp or Inkscape, create your "Pause" sprite.
The user should also know what to do in order to get back to the game, so either integrate a small help text beneath it like "Press 'P' key to continue" within the sprite or in a text box.
Save it as a .png file.
I've implemented the Pause function in the Ghost shooter game (see Ashley's Beginner's guide).
See that the HUD layer is active (tip: lock the other layers). That way, parallax is 0,0 and the pause button will always appear on one place.
Double-click on the layout and insert your sprite and position it in the middle of the screen at the top left quadrant of the Layout size (toggle the Background layer visibility to see the window size, in this case 640x480 and not 1280x1024!).
In the properties panel:
- set its "Name" to pauseButton
- set "Initial visibility" to invisible (alternatively, you can go to the Event sheet and make it invisible On start of layout)
- add a new instance variable called isPaused, Boolean type and initial value to false
Go to the Event sheet .
Create a new event for the keyboard:
Add condition Keyboard -> On key pressed -> P
NO action required yet.
Right-click on the green arrow at the left of the condition, go to Add and choose Add sub-event.
Add condition pauseButton -> Is boolean instance variable set -> isPaused
So this will check that if the 'P' key has been pressed, check the boolean value of the variable isPaused. Since the initial value is "false", we should invert the condition by right-clicking on the Is isPaused condition and choosing invert.
Now the actions will be activated if the key 'P' has been clicked and if the boolean value of isPaused is false.
Add the following actions:
- pauseButton -> Set boolean -> isPaused to True
- System -> Set time scale -> 0
- pauseButton -> Set visible -> Visible
- Text -> Set text -> "Press 'P' to continue"
So what will happen?
Everything freezes: player, bullets, monsters and explosions due to time scale=0
The Pause button is now visible and the Score text changes to Press 'P' to continue
Additionally, the boolean variable isPaused is now true
To continue playing, we need to add another condition.
Right-click on the left of the sub-event pauseButton - Is isPaused and add an 'Else' condition.
- pauseButton -> Set boolean -> isPaused to False
- System -> Set time scale -> 1.0
- pauseButton -> Set visible -> Invisible
- Text -> Set text -> "Score: "& Score
So, if the game is paused, the above actions will run only if the key 'P' has been pressed and the isPaused variable is True.
Everything starts moving and exploding, the Pause Button is removed and the Score information is shown again.
I've noticed that you can pause the game even after the player dies.
So, it makes now sense to give the player a health instance variable as well and only pause the game, if he is still alive!
Go to the layout area, click on the Player sprite, add in the Properties panel an instance variable called health with a value of 1. Construct won't get confused between health variables of the individual monsters and the player.
In the Event sheet, go to the condition Monster -> On collision with Player and add following action:
Player -> Subtract from -> health -> 1
In our newly created P-key keyboard event, add the following condition after
Keyboard - On P pressed by right-clicking on it and add an aditional event
Player -> Compare instance variable -> health -> Greater than -> 0
Another thing I found out when pausing the game:
a) The player still rotates toward the mouse angle
b) The player still can shoot!
The solution is to add the condition to check the pauseButton boolean variable isPaused to
a) System -> Every tick --> Player -> Set angle toward -> Mouse.X, Mouse.Y
b) Mouse -> On Left button Clicked --> Player -> Spawn Bullet
Don't forget to invert the condition (see the red 'x')!
Now, when the game is paused, the player won't rotate and cannot shoot anymore.
So you see what needs to be done in case a new menu or window is called
1. Check keyboard and mouse conditions and freeze them, if necessary
2. Check what other objects are doing during the break
3. Check the time running
Apparently tile movement gets a little buggy when pausing, even with debug preview pausing... I made a post with further details, if you think you may know how to help me, check it out!