Step 5 in Building a Shooter Game in AS3

February 10th, 2010

On to the final step in making our shooter! You can go back and check Step 1, Step 2, Step 3 and Step 4 if you missed something. In this step, we’re going to add a score and set up our rocket so it can be hit by the enemy ships. This one’s going to be pretty simple, the point of this whole tutorial was to show the basics of building a game in AS3. If you want to take it farther, it won’t be that difficult and will be a great learning experience.

The first thing we are going to do is use the Text Tool (T) to add a dynamic text block to the stage. I put mine in the bottom left, but you can put it anywhere. Make sure that it’s set to dynamic text, not static or input and give it an instance name of scoreTxt. Because it’s dynamic we can change the contents with ActionScript and it’s only going to take a few lines of code to do this. First, we need to add this to the bottom of the other variables:

1
2
//Sets the variable score as an integer
var score:int = 0;

Next, in the rocketMovement function, right before the closing bracket, add:

1
scoreTxt.text = "Score: " + score;

This sets the text inside our scoreTxt textfield with the text() method. We’re telling it to display the word Score plus our variable score, whose value we will assign next in our Enemy class. The reason at we are putting this line of code in the rocketMovement function is because we are listening for any changes to score every frame. We could create a new function just to update the score, but since rocketMovement is doing what we want anyway, we can just add it in there.

Now in the Enemy.as file we just have to had one little line of code. Put this in the moveEnemy function:

1
_root.score += 5;

Now, when the enemy ship is destroyed, the game will add 5 to the score.

The easiest way I’ve found to end the game is to use a Boolean. In the main code, I created a variable called dead, typed it to Boolean and set it to false:

1
var dead:Boolean = false;

Now go into the Enemy.as file and add this code to moveEnemy function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//tests to see if the enemy ship has hit the rocket movieclip
if (this.hitTestObject(_root.rocket))
{
    //if it has, dead is set to true
    _root.dead = true;
}
//check to see if dead equals true    
if (_root.dead == true)
{
    //remove listener and the ship from the stage
    removeEventListener(Event.ENTER_FRAME, moveEnemy);
    _root.removeChild(this);
    //add the game over text to the stage
    var gameOver:GameOver = new GameOver();
    _root.addChild(gameOver);
    gameOver.x = 125;
    gameOver.y = 100;
    //move the rocket off the stage
     _root.rocket.x = -100;
     _root.rocket.y = -100;
}

I’ve created a moveclip that says Game Over, I named it gameOver and checked the Export for ActionScript box. When an enemy ship hits our rocket, the game will remove all the ships from the stage, move our rocket off of it and add the Game Over movieclip. The reason we are moving the rocket movieclip off the stage instead of removing it is because we put it on the stage in our .fla, if we had added it with addChild() we could remove it easier, but it’s not that big of a deal.

That’s it! Now you should have the basics of a shooter game that you can build on, adding lives and more than one kind of enemy shouldn’t be too hard. I hoped this helped and now I have to figure out what to build next.

Leave a Reply

Your email address will not be published. Required fields are marked *