Tutorial: Make a Vertical Shooter in AS2 – Part 4
Categories: Flash
Table of Contents
Step 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!
i followed exactly again and now the enemy isnt disappearing when hit by bullets and only sometimes the bullets disappear
February 3rd, 2009 at 5:43 am
If you have any issues just download the source file
February 4th, 2009 at 6:28 pm
The Source file only gives me a Text File… Any Help?
February 14th, 2009 at 10:41 pm
Right Click and select “Save File As…” and then open it up in Flash.
February 15th, 2009 at 10:15 am
Hello Mr. Sun,
First, thanks for a great tutorial. I love the comments in the code explaining what things do. I find it very useful.
I would like to know if you could explain how to remove the enemies that are not shot from the stage. I find when I institute this code, the enemy movieclips remain and continue to drop, causing the game to lag immensely.
I will continue to look for a solution but since this is your code, I would love to know where you would place it. I am guessing it would be placed in the “enemy time if statement” but I am unsure. I have everything else working to my satisfaction, with the above exception.
any help is appreciated.
April 4th, 2009 at 12:38 pm
Hello again Mr. Sun,
I found the answer in your code. 😀
if(this._y >= _root.mcMain._y-30 && this._y <= _root.mcMain._y){
_root['en'+enID].removeMovieClip();
}
I placed this under the eight line of code from the previous page for “enemy time function”. works like a charm.
It was very simple and I can adjust it to remove it once it goes under the bottom border. Great tutorial and thanks for making it.
April 4th, 2009 at 1:31 pm
Hey can you please write your full code that you have in this part? Because I can’t get it to work.
April 12th, 2009 at 2:13 pm
when the bullets collide with an enemy all the bullets on stage are deleted. There must be a way to have only the collided one be deleted.
May 5th, 2010 at 2:40 am