## Tutorial: Make a Vertical Shooter in AS2 – Part 3

Written By MrSun at 8:03 am - Saturday, January 17th, 2009
Categories: Flash

### Step 3: Creating the Enemies

Well, now that we can make our lil’ guy shoot, we have to make something for him to shoot at! I’m going to first start by just drawing a little enemy guy. It won’t be too artistic.

Its dimensions are 30×30 if you wanted to know.

Now, do the same thing we did for the bullet, convert it to a MovieClip, and Export it for ActionScript.

Next, we’ll do something similar to what we did with the bullet. We’re going to add two timing variables again at the top of the code:

```//ENEMY TIMING VARIABLES
//how much time before another enemy is made
var enemyTime:Number = 0;
//how much time needed to make an enemy
//it should be more than the shooting rate
//or else killing all of the enemies would
//be impossible :O
var enemyLimit:Number = 16;
```

Now, in order to add this enemy to the stage, we have to program this into the onEnterFrame function:

```	enemyTime ++;//incrementing time for enemy
if(enemyTime == enemyLimit){//if enough time has elapsed
var enID:Number = Math.random(); //create a variable that we'll use at the enemy's id
//setting it's coordinates
_root['en'+enID]._x = int(Math.random()*Stage.width);//randomly within the boundaries
_root['en'+enID]._y = -50; //sets this offstage at first
_root['en'+enID].onEnterFrame = function(){//then give it some functions
this._y += 5;
}
enemyTime = 0;//reset the time
}
```

This concludes this part of the tutorial. Next time, we’ll program the enemies so they get shot!

#### The Final Product:

Source .fla File

Tagori:

Why is the source.fla wrong? i downloaded it and its a tower defense game in production.

Analizame.com:

the swf file is not from this tutorial (vertical shooter) it seems from a defender game

Drew:

Could you possibly post a way to make the enemies move horizontally as well as vertically, but not all in the same way? I have been trying to find a solution to this for some time but the best I can do is make them all move identically

farfenwaffle:

hey! I have a question, where am I putting the codes? I would assume either the frame or the hero movieclip

Tagori:

drew, you need to make the _x into _y and vice versa. also, if it isn’t where you want it, you may have to mess with the +,- signs it’s a simple thing to do, but it can get confusing, so i suggest making another file as a backup.

Tagori:

ok, i made a start screen and when i push enter, the game starts, but there is no enemy, so i push enter twice more (once to go back to start screen, and another to start it again) and the enemy is there finally, could u help me figure out what i did wrong?

raccoon:

make sure you name the movie clip and linkage mcEnemy, so far all code for this should be in the 1st frame of the actions layer

raccoon:

and when he says “we have to program this into the onEnterFrame function:” put it before the very last } in the total code