Advanced game pausing

Favourite 68 favourites
Tutorial written by Dan72Originally published on 14th, September 2013

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!

Create a Pause sprite

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.
[3]https://www.scirra.com/images/articles/Paused.png
Save it as a .png file.

Load Pause sprite into Construct 2

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!).

Pause Sprite Properties

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

[4]https://www.scirra.com/images/articles/Scirra_Tut_Pause_1.jpg


Events

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.
Add the following actions:
- 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.

Problem 1: Game can be paused after player dies

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
Open full size image

Problem 2: Mouse is still active

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
Open full size image

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.

Conclusion

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

Ta-daa!!! :-)

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

1
Tedg 9,893 rep

Thanks for this tutorial.

Saturday, September 14, 2013 at 5:19:12 PM
1
Lordshiva1948 44.3k rep

Thanks that is wonderful way to stop game and it works too

Sunday, September 15, 2013 at 9:26:55 AM
1
PixelGnome 1,959 rep

Thanks dan c: will help me more then the one i did XD i am such a noob in Cunstruct 2.....

Wednesday, September 18, 2013 at 6:14:54 PM
1
AbelaNET 14.9k rep

Thanks, useful additions.

Sunday, September 29, 2013 at 6:13:35 PM
1
Watsonid 1,131 rep

Great tutorial! Thanks a ton. :)

Monday, January 27, 2014 at 4:24:11 AM
1
smsanthosh 2,455 rep

nice one... thanks for sharing...

Monday, February 17, 2014 at 5:30:45 PM
1
Arrow X 2,398 rep

Thx very useful

Thursday, February 27, 2014 at 11:24:46 AM
2
68tm 444 rep

confused hahhaa. please add screenshot event sheet

Saturday, May 10, 2014 at 11:38:52 AM
1
29041982 5,567 rep

how about TOUCH pause and resume?
I don't know how to do it, I just doing it with manual.

If press Pause button = set position outside layout
> Resume button = inside layout

if touch Resume button = set position outside layout
> Pause Button = Inside layout

Is there any easy way?

Tuesday, June 17, 2014 at 4:54:08 AM
1
Lou Bagel 10.8k rep

I usually put almost all of my events, except menu events, in a group called gameplay. When paused set gameplay group to deactivated and reactivate on unpause. That will eliminate the need to add a condition to almost every event to check if paused.

Thursday, March 31, 2016 at 1:50:49 PM
0
Sigtrygg 446 rep

Thank you for publishing this helpful tutorial! :)

Best regards,

Sigtrygg

Tuesday, May 24, 2016 at 8:21:31 PM
0
booboo123 685 rep

hello

i made a game to android and when its running in the background the sound is still playing is there any options like when ther user not play and do some other works game automatically need to be paused

or

when user pres the home button or back button in mobile game automatically need to be paused

can you pleas tell me how to make this happen

Sunday, October 30, 2016 at 7:28:55 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.