Blog

08
Aug
Spaceship Tutorial Part 4: Scoring & lives
Tutorials

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!

Spaceship3

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:

Source
Source

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!

Replies

thor says: (reply)

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.

michael cole says: (reply)

how do you make an enemy shoot. These tutorials have been great by the way

KC says: (reply)

Hey Thor #1

I’ve made a piece of code that will fix the first of your problems. On your spaceship actions insert this to the bottom of it.

if(this._y >=431){
this._y -= 6;
}
if(this._y <=40){
this._y += 6;
}
}

john says: (reply)

great tutorial – i got a bit lost at the beginning of part 4
‘Place this code on the game frame.’

what exactly is the game frame – and where do I access it?

philip says: (reply)

why does it add score when i shoot and not when i hit an enemy, oh and is there a way to make a boom animation to show you killed it, thanks.

Jacob says: (reply)

Thank you so so so much :-) These tuts helped so much THANK YOU !!!

Mesai says: (reply)

I used your source to check if my code was right, but even with yours i still dont get the score and lives to show on screen.

I do die however if im hit 3 times

    Ben says: (reply)

    me too! it works but it doesn’t show!

ludo says: (reply)

Hi
Your tuto is very nice !
but i very want to know how the enemy can shoot bullet to me ?

Tks

ludo says: (reply)

i have download your source and your game not work
they have no live and score and no game over….

and the game i make with your tuto have the same problem with another :
the enemy dont disapear when he hit me

show !
but do someting ….. pls

sqigglez says: (reply)

i cant find the player MovieClip button or whatever it is to get to the next step

sqigglez says: (reply)

nvm i got it

eyelsi says: (reply)

it’s not working for me.
the score and lives numbers disappear if the value is changed. so its gone once my bullet hits the enemy, and the lives is just not showing anything in the first place because we change the value to 3

Kenney says: (reply)

Did you take a look at the source of copied the text fields from there? That might help. Also, make sure if you use a font that isn’t standard on computers you have to embed it.

sircom81 says: (reply)

Hey guys i found the answer to the problem with the lives and score text
to solve that problem you must set the character properties to a basic type of font
i made that setting to ” _sans ” font type
and it works great
good luck guys

Guhan says: (reply)

IT RULLZ !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Rey Philip Abad says: (reply)

Hi I was wondering could you help me? We have a project due and it’s about doing something like this but space invaders style and the enemies need to fire back and limited bullets. I’ve done limited bullets, but that’s all I was able to do :( could you maybe help me with a code that could make the enemies move like space invaders?

Writer says: (reply)

Hello

I made this game with using your tutorial:
http://www.matejgab.szm.com/applegame.html

But I have few problems. My hand is going out from screen… thats first problem :( Than if I have score 0 and lower apples I dont loose… I just still play :(

Here is code I used:

stop();

score = 0;

i = 0;
enemies = [];

spawnEnemy = function(){
_root.attachMovie(“enemy”, “enemy”+i, _root.getNextHighestDepth());
_root["enemy"+i]._x = random(400);
_root["enemy"+i]._y = 0;
enemies.push(“enemy”+i);

_root["enemy"+i].onEnterFrame = function(){
this._y += 3;

if(this.hitTest(_root.player)){
for(e = 0; e Stage.height){
for(e = 0; e < _root.enemies.length; e++){
if(_root.enemies[e] == this._name){
_root.enemies.splice(e, 1);
}
}

this.removeMovieClip();
}
}

i++;
}

onEnterFrame = function(){
if(score 29){
gotoAndStop(6);}
}
}

enemy_interval = setInterval(spawnEnemy, 1500);

I added this action at the end:

onEnterFrame = function(){
if(score < 0){
gotoAndStop(5);

Maybe there is some problem with it :(

Thanks for any help.

Elroy says: (reply)

Hey, similar problem to my comment on the previous tutorial. When the enemy gets to the top point of my player ship it simply disappears, no loss of life or anything. Even taking out the code to delete the movie once off the screen doesn’t stop it deleting itself. Any ideas?

Elroy says: (reply)

Hey, small problem once more. When the enemy gets to he top edge of my player movie it deletes itself, even if I have removed all the code to delete it is still disappears as soon as it reaches the player movie. No loss of life either, just beep, gone.

LAURA says: (reply)

Everything seem to work quite well. There seems to be only one problem. There is no boundary line. My ship seems to go out of the screen and I cant see them. @KC, I also tried your piece of code. But it doesn’t work.

Karaishi says: (reply)

I can get it so that when the score gets to 200, i can get it to go to the next frame (which is the game voer screen for now, but i plan on adding other levels), but i cant get it to go to the next frame if my lives get to 0, they just keep going down into the negatives. Its the same if I switch scores and lives around, the score will just keep going up, but the lives will work. Here’s the code I used…

}
onEnterFrame = function(){
if(lives 190){
nextFrame();
}
}

Karaishi says: (reply)

oops, i don’t know what happened there..here’s the code I used:

onEnterFrame = function(){
if(lives 190){
nextFrame();
}
}

Karaishi says: (reply)

it’s not letting me submit the code…

Kenney says: (reply)

Well I already see a mistake, you’re using the variables lives which isn’t the score ofcourse. Then, you’ve written “lives 190″ which doesn’t mean anything.

You can to do “lives == 190″ to let Flash check if lives is the same as 190. You can also use < = to see if lives is 190 or less, or >= to see if lives is 190 or more.

Hope that helps.

Strange.... says: (reply)

Hey i downloaded the source file and tried to run it on flash cs5. everything works fine apart from the score and lives. this was not very surprising because so far everytime i try to make a text box display a variable it has not done so. please help!

Thank you Kenney, you saved me all the time trying to start an engine from scratch. Really grateful towards you :D

@LAURA:
Replace your existing movement code with this:

if(Key.isDown(Key.LEFT)){
if(_x > 50){
_x -= 3;
}
}else if(Key.isDown(Key.RIGHT)){
if(_x 50){
_y -= 3;
}
}else if(Key.isDown(Key.DOWN)){
if(_y < 350){
_y += 3;
}
}

This assumes that your screen is 550×400, and only allows you to move the plane when your x_axis of the plane is 50pixels from the screen boundary(you need to take into account the distance from the centre of the plane to it's tip of it's end or half it's body will be outside)

Also, the code might not work because you are using CS5 instead of CS4, this happened to my game.(http://www.flashaze.110mb.com/index.php?p=1_2_Mush-Tree-Tutorial) You can try fixing this problem by saving the game .fla in CS4 format instead of CS5 format.

my bad, the code should be:

if(Key.isDown(Key.LEFT)){
if(_x > 50){
_x -= 5;
}
}else if(Key.isDown(Key.RIGHT)){
if(_x < 500){
_x += 5;
}
}

only, i had the extra bit because i am making a horizontal sidescrolling shooter using this.

PAR says: (reply)

Where is the main game frame ??

PAR says: (reply)

hi there, i really need ur help please i have completed making this wonderful game.But i need some help on how to put lives and the points into the game.Based on the explanation above,it says tht i have the code into the “main game frame”.
I am a little confuse on this and what does the “main game frame” means ??
i really wish if someone could help me

Kenney says: (reply)

The main game frame is the frame of your game on which you coded the game.

PAR says: (reply)

thanks for ur reply..but i still dont really know how do i put the code into the Game Frame like how u said

” Place this code on the game frame.”

Kenney says: (reply)

Click on the frame in the timeline, then hit F9 and place the code in the code window.

Joe Sorestad says: (reply)

Hi This tutorial is great and I thank you for it, but I can’t make the score boards properly display the score when you shoot enemies, nor do the lives count down properly. Which part of the code should I try looking at fixing?

khupertz says: (reply)

same problem with Joe.

PAR says: (reply)

how do i create a Start screen ??

Kenney says: (reply)

Just create a new frame like you did with the Game Over screen only make sure it’s before the game screen. Then, make a button (just like a MovieClip only select Button) and put this code on it:

on(press){
nextFrame();
}

PAR says: (reply)

thanks kenney..tht really did it..thank u soo much ..now is it possible when i shoot the enemies..instead of dissapearing i want it to show some explosion ..

thanks anyways..=)

jacd says: (reply)

hey..you gave the code above to go to the next frame when on the start screen. But I want to click at the button instead of pressing the enter button to move to next frame. Could you provide me some code for it please.

Kenney says: (reply)

@PAR, you’d have to make a second frame INSIDE the enemy. Then, instead of removing the enemy (see code and my comments) you have to tell the enemy to go to the next frame. Same code as above, nextFrame(); or directly tell him to go to an exact frame: gotoAndStop(2); where 2 is the frame you’d want it to do.

@jacd, Watch out that the enter to go the next frame only works within your Flash editor, it won’t work for people if they play your game in the browser. I explained how to make a button in my previous comment. Just like a MovieClip, only tick Button instead of MovieClip and place the above code on the button.

khupertz says: (reply)

Help sir,I follow all the steps, but i can’t make the score boards properly display the score and the lives count down like Joe Sorestad

khupertz says: (reply)

Don’t need to answer my question sir.. I just did it when i read all the previous comments and it work when i change font family to “_sans” .
Anyway thanks for this great tutorial.

by the way, i want to add some environment on the screen.. Is there any code on like a different environment is coming? For example, first environment is “building” then the second one is “sea” then “mountain”. And the spaceship is like moving forward.
Hope you help me sir. thanks

kuddleitz says: (reply)

i followed all your intstructions, it came out good but my ship is not dying. this is the code i used from you tuts:

onClipEvent(load){
timer = 0;
i = 0;
}

onClipEvent(enterFrame){
if(timer > 0){
timer–;
}

if(Key.isDown(Key.LEFT)){
_x -= 6;
}else if(Key.isDown(Key.RIGHT)){
_x += 6;
}

if(Key.isDown(Key.SPACE)){
if(timer == 0){
_root.attachMovie(“bullet”, “bullet”+i, _root.getNextHighestDepth());
_root["bullet"+i]._x = _x;
_root["bullet"+i]._y = _y-20;

_root["bullet"+i].onEnterFrame = function(){
this._y -= 10;
if(this._y < -30){
this.removeMovieClip();
}

for(k = 0; k < _root.enemies.length; k++){
if(this.hitTest(_root[_root.enemies[k]])){
_root.score += 10;
this.removeMovieClip();
_root[_root.enemies[k]].removeMovieClip();
_root.enemies.splice(k, 1);
}
}
}

i++;
timer = 20;
}
}
}

Werwar says: (reply)

yep..same problem…immortal ship

shawny boy says: (reply)

thanx for the tut. i had no promblems with it. i wish other tuts were just as easy. thanx again

Kropljotaajs says: (reply)

Thank u so much for these tutorials !!!!!

I hope u make a 2D rpg shooter tutorial or a Platform game tutorial :)

Thank u SO much !!!!!!!!!!!

sysy says: (reply)

how can the score and lives work???
can you give me the whole code of it…..

Kenney says: (reply)

The whole code is above.

ROCK says: (reply)

Hi kenney, thank u for the tutorials. By the way i have done everything now i’m struck with ending the game. for my college assignment i need to create a game with minimum 1 level so far i have done everything and its working fine also but could u please help me how to end the game after a certain kills of enemy by saying that level 1 complete. it will be very helpfull if u guide me a bit more. i need to submit the assignment by this thursday i’m really sorry to distrub u bro. thanx.

Liam says: (reply)

Great tutorial, except you should go back and look at your life and score text.

sircom81 – was spot on with what the problem was.

Kenney says: (reply)

Well, sircom81′s comment isn’t really a solution. The problem is that if you use a font that’s not a default font on computers you have to embed the characters. Search for that on Google.

cian2d2 says: (reply)

Hi my enemys just fly through me and my lives don’t work heres the code
player:
onClipEvent(load){
timer = 0;
i = 0;
}

onClipEvent(enterFrame){
if(timer > 0){
timer–;
}

if(Key.isDown(Key.LEFT)){
_x -= 6;
}else if(Key.isDown(Key.RIGHT)){
_x += 6;
}

if(Key.isDown(Key.SPACE)){
if(timer == 0){
_root.attachMovie(“bullet”, “bullet”+i, _root.getNextHighestDepth());
_root["bullet"+i]._x = _x;
_root["bullet"+i]._y = _y-20;

_root["bullet"+i].onEnterFrame = function(){
this._y -= 10;
if(this._y < -30){
this.removeMovieClip();
}

for(k = 0; k Stage.height){
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–;
}
for(e = 0; e < _root.enemies.length; e++){
if(_root.enemies[e] == this._name){
_root.enemies.splice(e, 1);
}
}

this.removeMovieClip();
}
}

i++;
}
onEnterFrame = function(){
if(lives < 0){
nextFrame();
}
}
enemy_interval = setInterval(spawnEnemy, 2000);

cian2d2 says: (reply)

sorry don’t think my main code posted properly

stop();
i = 0;
enemies = [];
lives = 3;
score = 0;
spawnEnemy = function(){
_root.attachMovie(“enemy”, “enemy”+i, _root.getNextHighestDepth());
_root["enemy"+i]._x = random(Stage.width);
_root["enemy"+i]._y = 0;
enemies.push(“enemy”+i);

_root["enemy"+i].onEnterFrame = function(){
this._y += 3;
if(this._y > Stage.height){
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–;
}
for(e = 0; e < _root.enemies.length; e++){
if(_root.enemies[e] == this._name){
_root.enemies.splice(e, 1);
}
}

this.removeMovieClip();
}
}

i++;
}
onEnterFrame = function(){
if(lives < 0){
nextFrame();
}
}
enemy_interval = setInterval(spawnEnemy, 2000);

Kenney says: (reply)

Again, just check out the source to see what you did wrong. I don’t have time to check your code.

Eddie says: (reply)

Scoreboard doesn’t work

Kenney says: (reply)

CHECK. OUT. THE. SOURCE to find out your problem.

josh says: (reply)

tell me ur email so that i can send you my plash and u can fix it! the lives dont work! please!

josh says: (reply)

flash*

Kenney says: (reply)

I don’t do that.

NESH says: (reply)

Hi Kenney, can i know how to stop enemies respawn after a certain time. thank you

Kenney says: (reply)

Yeah, just combine some of the things I teach in my tutorials. That’s all you need!

joe says: (reply)

can anyone convert this to as3 code?

jene says: (reply)

how to make an enemy to shoot bullets.

Vishal Shrivastav says: (reply)

Hello Kenney, how can i make levels in this game.
after each level the speed of the enemy increase.

Pls help..

Vishal Shrivastav says: (reply)

Hello Kenney,
Thanx for helpful tutorial …

i want to make level in this game and after each level the speed of enemy increase.

pls help…



Leave a Reply