In the final part of the tutorial on how to make a Flash game we’re gonna finish up the game with scoring, lives and ofcourse a way to lose the game. Make sure you read the previous parts first!
If you haven’t read the previous parts first, go ahead and read part 1, part 2 and part 3 of the tutorial to learn how to create and move the spaceship, make it shoot and spawn enemies.
Step 1. Preparing the variables
First we have to set-up some variables in which we’re going to save some information. So let’s do that first:
1 2 | lives = 3; score = 0; |
Place this code on the game frame. You can change the number of lives the player has by simply changing the number 3 in the lives variable.
Step 2. Changing variables based on game events
Now we have to think when the variables need to be changed. Lives should be changed when the player hits an enemy, score should be changed when the player shoots an enemy. Let’s see if we can code that! Find the following line of code in the script we placed on the player MovieClip:
1 | if(this.hitTest(_root[_root.enemies[k]])){ |
Then directly under it add this line:
1 | _root.score += 10; |
This tells to add 10 to the score variable which is placed in the root (main timeline). Next, find this line in the script on the main game frame:
1 | if(this._y > Stage.height){ |
and add this above it:
1 2 3 4 5 6 7 8 9 10 | if(this.hitTest(_root.player)){ for(e = 0; e < _root.enemies.length; e++){ if(_root.enemies[e] == this._name){ _root.enemies.splice(e, 1); } } this.removeMovieClip(); _root.lives--; } |
Big block of code but it’s almost the same as the one under it. So if an enemy hits the player, it should remove itself (from the enemies array AND the stage) and subtract 1 (remember, — means -1) from the lives variable on the main timeline. But wait a second, the game doesn’t know what ‘player’ is yet so it won’t work! Click on the player MovieClip and find the ‘< Instance Name >‘ input field in the properties plane, enter “player” there without the quotes.

Now we have to make the game check the lives and if the player is out of lives show a game over screen. Find the following line of code on the game frame (should be the bottom line):
1 | enemy_interval = setInterval(spawnEnemy, 2000); |
then add this above it:
1 2 3 4 5 | onEnterFrame = function(){ if(lives < 0){ nextFrame(); } } |
Some lines of this code may seem familier while others are new, so let me explain:
1 | onEnterFrame = function(){ |
This tells the game to execute the following lines of code every frame, so if you have set the FPS of the game to 45 then the following code is going to be executed 45 times per second.
1 | if(lives < 0){ |
If the lives variable is below 0…
1 | nextFrame(); |
Go to the next frame in your game, we’ll talk about that in a bit.
Okay, the coding is done but we still have to show the player how many lives and what score he or she has. For that we have to add text fields to the game and a second frame.
Step 3. Text fields
Select the Text tool (press T on your keyboard) in the tools panel then click anywhere on the stage and start typing to create a text field. There are options to change the font, color, size and more in the Properties pane (CTRL + F3). Make a couple of text fields like I did:

Now select the text field under “Score:”, it should say 0. Then in the Properties pane change the value Static Text to Dynamic Text and at the ‘Var:’ input field fill in “score” (without quotations). Also select the text field that says 0 under lives and make it a Dynamic Text with var “lives” (again, without quotations
).

If you test your game now you can see that it actually adds score if you shoot an enemy and it subtracts a life if an enemy hits you!
Now the only thing left to do is create a Game Over screen!
Step 4. Game Over screen
But before we move on creating a new game screen we’d want the game to keep showing the game frame first, so add this code above all the other code on the game screen:
1 | stop(); |
This tells the game to stop at this screen and not move to the Game Over screen until we tell it to do so. While you’re in the code on the game frame hit F7 to create a new frame (or game screen). On that frame add the following command again:
1 | stop(); |
Now make yourself a nice Game Over screen, you can use text like we did above or draw something using the other tools Flash offers. I made this one (I’m sure you can do much better!):

Now test your game again. Oh, what’s that? There’s a problem? Well yeah there is. The enemies keep appearing at the Game Over screen! Add this code to the Game Over screen to stop them:
1 | clearInterval(enemy_interval); |
This tells Flash to remove the interval we created before that creates the enemies.
Step 5. Testing and finishing
Now press CTRL + ENTER to test the game, you should go to the Game Over screen if you lose all your lives! If you had any problems feel free to ask questions below, if you want a sample download the source file here:
This is the final part of my tutorial series. If you’d like to learn more about Flash I recommend you to buy or rent a book. They’re so much better than any tutorial out there since they’re well written and explain everything there is to know from A to Z. That’s how I learned it!

Home
Games
Portfolio
Tutorials
Mobile
Contact









hey i have three problems
1 how do you make walls so youre player dosent go out of they screen.
2 is there a way that i might be able to make levels .
3 is there a way to spawn more than 1 type of enemy.