Developing a space shooter game [ October 07, 2004 ] by Richard Nias aka Crashlanding
In this collection of tutorials, the author explains how to create a basic shooter game, considering all its different aspects: movement, shooting, enemies, sound, etc.
Many times a game is made, but it can't pause. The game could be a really addictive game, win a handful of awards and get your website 1000 hits a day but some people will always say "Nice game, great concept but... you need a pause button". It may be a minority of people who criticize it but in the end, but some people won't play the game as much. But now, after this tutorial you will know a code that will always work, no matter what game you are making. So, lets get started and see what we're doing. Press P to pause.
The flashing text is an add-on, but if you do decide to have it, make a movieclip with the animation inside it and put it in the library with the linkage name of "pause". Then, put at the beginning of the script:
This attaches the pause movieclip at a depth of 1000, just so its above the enemies and bullets, and makes it invisible. But, because our bullets depth equals i*100, only the first few bullets will be below it and the tenth bullet will replace the pause movieclip altogether. So, we need to stop the tenth bullet being created with a depth of 1000. So put in the bullet function before a new bullet is attached:
if (i == 10)
i = 0;
Now that we've sorted that out, we can move back to pausing. First we need a variable to tell us whether the game is paused which will change from true to false when P is pressed and vice-versa. Just after you attach the pause movieclip, before the moveHero function, put:
var paused = false;
If you want the game to start paused set it to true. Now change the onEnterFrame bit at the bottom to:
Now you need to surround the actions for the bullets and enemies with:
if (paused == false)
So now nothing will happen unless paused=false. So we need to add the ability to change paused so it equals true. We could use what we did for all the other key detections:
if (_root.paused != true)
_root.paused = true;
_root.paused = false;
If you held down P, every 25th of a second the game would be flipping between paused and unpaused. It would be better if it only changed when the key was released. Flash can do this, but it is a bit more complicated and uses something called key listeners...
var listener = new Object();
listener.onKeyUp = function()
var code = Key.getCode();
if (code == 80)
if (paused == false)
paused = true;
paused = false;
First a new object is made called listener. Key listeners have to be objects, so it is important you make an object. We then use the key objects onKeyUp listener to detect when a key is released. Then a variable containing the key code of the key that was released is defined, and used to find out if the key released was P (80 is P's key code). If it is P, the paused variable is changed depending on its current value. Then at the end, listener is added as a key listener to the key object.
Remember, because a key listener can detect onKeyDown as well so could be used for all your key detections in this game. For future reference, although these and all other key codes are stored in the flash help files, the key codes you would need are:
So, now your game is paused. If you have any problems, email me at firstname.lastname@example.org and you can download the source complete with comented code and graphics, click here.
Richard Nias aka Crashlanding
South-east London, UK
Had Flash for 1 1/4 years now. I learnt from the help files before finding gotoandplay and flashkit, getting me intrested in games.