How to Make a Vertical Shooter in AS2 – Part 4
Part 4: Programming the Enemies
Now we have to make it so the enemies can be shot. This probably will be the most complicated of the steps. But, it also is what makes this thing a game. There are two ways that I know of to accomplish this task. The first one is to have the bullet listen for hit testing the enemy, or the enemy listen for the bullet. It’s basically the same thing. However, I’m going to have the enemy listen for the bullet, just because I feel like it.
The first thing we need to do in order to accomplish this feat is to create a container for all of the bullets. This way, we can access all of the bullets through the container. I can’t really explain this too well, so I’ll just demonstrate it for you. At the top of the code, add the following:
//this movieclip will hold all of the bullets
_root.createEmptyMovieClip('bulletHolder', _root.getNextHighestDepth());
The bulletHolder is invisible and is just a container for all of the bullets. In order for this to be any use to us, we need to actually add the bullets to this container. This will be easy. Find where we add the bullet to the stage:
_root.attachMovie('mcBullet', 'Bullet'+bulletID,_root.getNextHighestDepth());
//setting the coordinates of the bullet to be the same as the main character
_root['Bullet'+bulletID]._x = mcMain._x + mcMain._width/2 - _root['Bullet'+bulletID]._width/2;
_root['Bullet'+bulletID]._y = mcMain._y;
_root['Bullet'+bulletID].onEnterFrame = function(){
//giving the bullet some actions
this._y -= 10; //moving the bullet
if(this._y < -1 * this._height){//if the bullet goes off stage
//then destroy it
this.removeMovieClip();
}
}
Change it to this:
bulletHolder.attachMovie('mcBullet', 'Bullet'+bulletID,_root.getNextHighestDepth());
//setting the coordinates of the bullet to be the same as the main character
bulletHolder['Bullet'+bulletID]._x = mcMain._x + mcMain._width/2 - bulletHolder['Bullet'+bulletID]._width/2;
bulletHolder['Bullet'+bulletID]._y = mcMain._y;
bulletHolder['Bullet'+bulletID].onEnterFrame = function(){
//giving the bullet some actions
this._y -= 10; //moving the bullet
if(this._y < -1 * this._height){//if the bullet goes off stage
//then destroy it
this.removeMovieClip();
}
}
Now we enter complicated territory. Find the onEnterFrame function we added to the Enemy. Add the following to it:
//run a loop checking if it's touching any bullets
for(var cBullet:String in _root.bulletHolder){
//if it's touching the bullet
//we have to use coordinates because hit testing doesn't seem to work
if(this._y >= _root.bulletHolder[cBullet]._y-30 && this._y <= _root.bulletHolder[cBullet]._y){
if(this._x <= _root.bulletHolder[cBullet]._x+5 && this._x >= _root.bulletHolder[cBullet]._x -35){
//then destroy this guy
_root['en'+enID].removeMovieClip();
//and destroy the bullet
_root.bulletHolder[cBullet].removeMovieClip();
}
}
}
Now, try out your game. It should work pretty well now. The only problem with it, however, is that it doesn't do anything when it touches the player. We aren't going to really do anything with this right now, but we will set it up so we can make a function for it in the next part of the tutorial, "Scoring". Just place the following code in the Enemy's onEnterFrame() function:
//hit testing with the user
if(this._y >= _root.mcMain._y-30 && this._y <= _root.mcMain._y){
if(this._x <= _root.mcMain._x+30 && this._x >= _root.mcMain._x -30){
//we'll add code here in the next part
//but for now, we'll just trace something
trace('You got hit!');
}
}
Well, that's all for today. Join us next time when we score the game!