How to Make a Vertical Shooter in AS3 – 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.
My enemy
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. This time, give it a class of “Enemy”. I think you know what’s next. Now, we have to create another external file for the enemy. Once you’ve done that, place in the following code:

//This is the basic skeleton that all classes must have
package{
	//we have to import certain display objects and events
	import flash.display.MovieClip;
	import flash.events.*;
	//this just means that Eullet will act like a MovieClip
	public class Enemy extends MovieClip{
		//VARIABLES
		//this will act as the root of the document
		//so we can easily reference it within the class
		private var _root:Object;
		//how quickly the enemy will move
		private var speed:int = 5;
		//this function will run every time the Bullet is added
		//to the stage
		public function Enemy(){
			//adding events to this class
			//functions that will run only when the MC is added
			addEventListener(Event.ADDED, beginClass);
			//functions that will run on enter frame
			addEventListener(Event.ENTER_FRAME, eFrame);
		}
		private function beginClass(event:Event):void{
			_root = MovieClip(root);
		}
		private function eFrame(event:Event):void{
			//moving the bullet up screen
			y += speed;
			//making the bullet be removed if it goes off stage
			if(this.y > stage.stageHeight){
				removeEventListener(Event.ENTER_FRAME, eFrame);
				_root.removeChild(this);
			}
		}
	}
}

If you didn’t notice, I just copied and pasted the code from “Bullet.as” and edited it around a little bit. You’ll find that you will do this often when making classes. This code will just make the Enemy just move down the screen. Now, we have to add code to the main timeline that will create it and add it to the stage.

We first have to add 2 variables at the top of our code. They will be similar to cTime and cLimit but instead they are for creating enemies. Here’s the code:

//how much time before another enemy is made
var enemyTime:int = 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:int = 16;

Next, we have to create a function that will add the enemies to the stage. We don’t really want too many enterFrame functions so we can just add to the moveChar function. If you want, you can rename it to eFrameFunctions so that you don’t get confused on the purpose of the code. Add this to the bottom of that function.

//adding enemies to stage
if(enemyTime < enemyLimit){
	//if time hasn't reached the limit, then just increment
	enemyTime ++;
} else {
	//defining a variable which will hold the new enemy
	var newEnemy = new Enemy();
	//making the enemy offstage when it is created
	newEnemy.y = -1 * newEnemy.height;
	//making the enemy's x coordinates random
	//the "int" function will act the same as Math.floor but a bit faster
	newEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));
	//then add the enemy to stage
	addChild(newEnemy);
	//and reset the enemyTime
	enemyTime = 0;
}

This code will constantly add the enemies to the stage, which is exactly what we want. This ends this part of the tutorial. Next time, we'll program the enemies so they get shot!

Preview

Download Source
(Requires Flash CS3 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials