Tutorial: Make a Vertical Shooter in AS3 – Part 4


Written By MrSun at 8:03 am - Saturday, August 23rd, 2008
Categories: All Tutorials, AS3, Flash, Game Development, Intermediate Tutorials

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.

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
var bulletContainer:MovieClip = new MovieClip();
addChild(bulletContainer);

The bulletContainer 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. Instead of the code where we add the bullet to the stage, addChild(newBullet);, we just replace it with bulletContainer.addChild(newBullet). It’s as simple as that. But now arises a problem. When we remove the MovieClip from stage, it returns an error. This is simple to fix. Instead of saying just _root.removeChild(this) in the “Bullet.as” file, replace it with _root.bulletContainer.removeChild(this);. Just another simple quick fix.

Now we enter complicated territory. Go to “Enemy.as” and add the following code to the eFrame() function:

//checking if it is touching any bullets
//we will have to run a for loop because there will be multiple bullets
for(var i:int = 0;i<_root.bulletContainer.numChildren;i++){
	//numChildren is just the amount of movieclips within 
	//the bulletContainer.
 
	//we define a variable that will be the bullet that we are currently
	//hit testing.
	var bulletTarget:MovieClip = _root.bulletContainer.getChildAt(i);
 
	//now we hit test
	if(hitTestObject(bulletTarget)){
		//remove this from the stage if it touches a bullet
		removeEventListener(Event.ENTER_FRAME, eFrame);
		_root.removeChild(this);
		//also remove the bullet and its listeners
		_root.bulletContainer.removeChild(bulletTarget);
		bulletTarget.removeListeners();
	}
}

You might note that we have to run removeListeners() which isn’t a preset ActionScript function. We have to define it in the Bullet class because there isn’t any other way to remove the listeners. Just define the function in the “Bullet.as” file.

public function removeListeners():void{
	removeEventListener(Event.ENTER_FRAME, eFrame);
}

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 eFrame() function:

//hit testing with the user
if(hitTestObject(_root.mcMain)){
	//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!

The Final Product:

Source Files (zipped)

4 Comments

Danilo:

Hi, I’ve been learning AS3 for a while, and the most important thing I could never figure out is how to code bullets and those things, mainly because there are always many bullets and I don’t know how to code them so they hit the enemy.

I see you handled this creating the “bulletContainer” var and using the loop “for(var i:int = 0;i<_root.bulletContainer.numChildren;i++)”
My problem is, I know this works, but I don’t know how, I mean, I don’t know how the “bulletTarget” var works, so it would be appreciated if you could explain this.

Anyway, thanks a lot for all the help :)


Mr Sun:

Hey Danilo. I know the hardest part of learning and teaching ActionScript or any kind of programming language is to learn how the code actually works. I’ll try to explain it as best I can.

In ActionScript, you can assign certain MovieClips that are already placed on stage, a variable name. For example, if you put a MovieClip with a instance name of mcThing onto the stage without using any code, you could give it a variable name in code like so:

var thingVariable:MovieClip = mcThing;

This way, you can access the MovieClip’s properties by using either thingVariable or mcThing. This is sort of how the bulletTarget works, except more dynamically.

In our for loop, we define the limit as i<_root.bulletContainer.numChildren. This means that this loop will run through for every single MovieClip that is in the bulletContainer, which would just be for all of the bullets.

getChildAt() simply means that bulletTarget will be the MovieClip that is at the certain index level as the i variable. Index level isn’t too important, it’s similar to depth in AS2.

Hopefully, this is a bit of help to you. Good luck on learning ActionScript!


Jonathan Persson:

Hey!
Thank you for this (atleast this far ;P) wonderful tutorial. It’s just that i got a little problem. If i try to run the game (after i added the trace and the hitTestObject thing) i only get hit if i shoot at the same time as i am hit, with mcMain… And if i just stay still with mcMain, the bullets don’t hit, they go straight through mcMain.


Tyler:

Great tutorial thank you for doing it. I understand the basic movement in the first part but i just don’t know what everything else means and why you put it there.


«
»