How to Make a Vertical Shooter in AS2 – Part 3
Part 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
_root.attachMovie('mcEnemy', 'en'+enID,_root.getNextHighestDepth());//then add the enemy
//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!