How to Make a Vertical Shooter in AS3 – 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.

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!

Preview

Download Source
(Requires Flash CS3 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials