How to Create a Platform Game in AS3 – Part 5

Part 5: Adding Enemies

The next step in making our exciting platform game is to add some enemies! In order to do this, we’re first going to have to make an Enemy class. We can do this by creating an external ActionScript file and naming it Enemy.as. Then, place the following code into the file. It’ll be similar to the “Block.as” file that we made earlier.

package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.DisplayObject;
	import flash.events.*;
 
	//sprites are just movieclips without any frames in them
	public class Enemy extends Sprite{
		//_root will signify the root of the document
		private var _root:Object;
		//how quickly the enemy can move
		private var speed:Number;
		//-1 is left, 1 is right
		private var direction:int;
 
		public function Enemy(){
			//this code will only be run once
			addEventListener(Event.ADDED, beginClass);
			//this code will constantly be run
			addEventListener(Event.ENTER_FRAME, eFrame);
		}
 
		private function beginClass(event:Event):void{
			//defining the root of the document
			_root = MovieClip(root);
			//defining the movement variables of the enemy
			speed = 5;
			direction = 1;
			//drawing the enemy (it'll be a red circle)
			this.graphics.beginFill(0xFF0000,1);
			this.graphics.drawCircle(12.5,12.5,12.5);
		}
 
		private function eFrame(event:Event):void{
 
		}
	}
}

All this code does is add the enemy to the stage and make it into a red ball. Before we actually add the enterFrame programming to it, I want to first add some code to the main timeline. This code will just add the enemy to stage and also some markers which will tell the enemy when to turn around. Add the following to the for loop in the createLvl() function:

if (lvlArray[i] == 5){
	//adding an enemy
	newPlacement = new Enemy();
	enemyHolder.addChild(newPlacement);
} else if (lvlArray[i] == 6){
	//adding an invisible marker for the enemy to turn around
	newPlacement = new Shape();
	newPlacement.graphics.beginFill(0x000000,0);
	newPlacement.graphics.drawRect(0,0,25,25);
	invisMarkerHolder.addChild(newPlacement);
}

Of course, we’re going to have to add both an invisMarkerHolder and an enemyHolder. Hopefully, you can do this on your own. Just do the exact same thing we did with the other holders except with that name. Next, change the level array to this:

var lvlArray1:Array = new Array(
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
);

We can now add some real programming to the Enemy. Add the following code to the eFrame() function in Enemy.as:

private function eFrame(event:Event):void{
	//MOVING THE ENEMY
	this.x += speed * direction;
	//checking if touching any invisible markers
	for(var i:int=0;i<_root.invisMarkerHolder.numChildren;i++){
		//the process is very similar to the main guy's testing with other elements
		var targetMarker:DisplayObject = _root.invisMarkerHolder.getChildAt(i);
		if(hitTestObject(targetMarker)){
			direction *= -1;
			this.x += speed * direction;
		}
	}
	//TOUCHING THE MAIN CHARACTER
	if(this.hitTestObject(_root.mcMain)){
		//if it touches the main guy while he's jumping
		//and the guy is falling down, not jumping up
		if(_root.mainJumping && _root.jumpSpeed > 0){
			//kill this guy
			this.parent.removeChild(this);
			//and remove listners
			this.removeEventListener(Event.ENTER_FRAME, eFrame);
		} else {
			//otherwise, kill the main character
			//we don't have any death frame yet, so we'll make it later
			//and we'll add a trace statement
			trace('OH NO! You died!');
		}
	}
}

Test it out. You should see a red circle moving around the platform above where you start off. If you touch it, a trace statement should be called. If you jump on it, the enemy should disappear. Pretty nifty, eh? The next thing we have to do is to make a function that will reset the game whenever the main guy dies. Add this code to the main timeline:

//resets the level
function resetLvl():void{
	for(var i:int=0;i<lvlHolder.numChildren;i++){
		//selecting the current container
		var currentContainer = lvlHolder.getChildAt(i);
		//and deleting all of it's contents
		while(currentContainer.numChildren > 0){
			for(var i2:int = 0;i2<currentContainer.numChildren;i2++){
				currentContainer.removeChildAt(i2);
			}
		}
	}
	//then we remake the lvl and reset the lvlHolder
	lvlHolder.x = 0;
	createLvl();
}

Now, we have to run this function whenever the main guy runs off of the stage or touches the enemy. I’m going to leave it up to you to figure that out. Of course, there are always the source files if you need them.

Well, that’s basically it for enemy creation. Join us next time when we actually make this thing into a game!

Spread the Knowledge

  • Print
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Twitter
  • Google Bookmarks
  • Design Float
  • DZone
  • Yahoo! Buzz
  • PDF
  • Reddit
  • RSS
  • Technorati

Preview

Download Source
(Requires Flash CS3 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials