Tutorial: Create a Platform Game in AS2 – Part 5


Written By MrSun at 8:04 am - Saturday, January 03rd, 2009
Categories: Flash

Step 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 MovieClip. Mine is just going to be a red circle. Then, we’re going to turn it into a MovieClip called “mcEnemy”.

Next, I’m just going to make a mcInvisMarker that will tell the enemy when it should turn around when it moves. It’ll be invisible, so I’m going to make it a square with the alpha set to 0. Then, I’ll just make it into a MovieClip that’s exported for ActionScript.

Now, we have to add some code to the createLvl() function. This code will just add the enemy to stage and also some the invisible markers. Add the following to the for loop in the createLvl() function:

 else if (lvlArray[i] == 5){
	lvlHolder.enemyHolder.attachMovie('mcEnemy', 'Enemy'+i,lvlHolder.enemyHolder.getNextHighestDepth());
	lvlHolder.enemyHolder['Enemy'+i]._x = (i-(row-1)*lvlColumns)*25;
	lvlHolder.enemyHolder['Enemy'+i]._y = (row-1)*25;
} else if (lvlArray[i] == 6){
	lvlHolder.markerHolder.attachMovie('mcInvisMarker', 'Marker'+i,lvlHolder.markerHolder.getNextHighestDepth());
	lvlHolder.markerHolder['Marker'+i]._x = (i-(row-1)*lvlColumns)*25;
	lvlHolder.markerHolder['Marker'+i]._y = (row-1)*25;
}

Of course, we’re going to have to add both a markerHolder 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,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,0,0,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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 createLvl() function where we add the enemy to the stage:

if (lvlArray[i] == 5){
	lvlHolder.enemyHolder.attachMovie('mcEnemy', 'Enemy'+i,lvlHolder.enemyHolder.getNextHighestDepth());
	lvlHolder.enemyHolder['Enemy'+i]._x = (i-(row-1)*lvlColumns)*25;
	lvlHolder.enemyHolder['Enemy'+i]._y = (row-1)*25;
	lvlHolder.enemyHolder['Enemy'+i].speed = 5;
	lvlHolder.enemyHolder['Enemy'+i].direction = 1;
	lvlHolder.enemyHolder['Enemy'+i].onEnterFrame = function(){//here's the function to program the enemy
		//MOVING THE ENEMY
		this._x += this.speed * this.direction;
		//checking if touching any invisible markers
		for(var cMarker:String in lvlHolder.markerHolder){
			//the process is very similar to the main guy's testing with other elements
			if(this.hitTest(lvlHolder.markerHolder[cMarker])){
				this.direction *= -1;
				this._x += this.speed * this.direction;
			}
		}
		//TOUCHING THE MAIN CHARACTER
		if(this.hitTest(_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.removeMovieClip();
			} 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{
	lvlHolder.removeMovieClip(); //destroy the lvl holder and everything in it
	//then we add everything back again
	_root.createEmptyMovieClip('lvlHolder', _root.getNextHighestDepth());
	lvlHolder.createEmptyMovieClip('blockHolder',lvlHolder.getNextHighestDepth());
	lvlHolder.createEmptyMovieClip('ladderHolder',lvlHolder.getNextHighestDepth());
	lvlHolder.createEmptyMovieClip('bumperHolder',lvlHolder.getNextHighestDepth());
	lvlHolder.createEmptyMovieClip('trampHolder',lvlHolder.getNextHighestDepth());
	lvlHolder.createEmptyMovieClip('enemyHolder',lvlHolder.getNextHighestDepth());
	lvlHolder.createEmptyMovieClip('markerHolder',lvlHolder.getNextHighestDepth());
	//then we remake the lvl and reset the main's existence
	createLvl();
	mcMain.swapDepths(getNextHighestDepth());
}

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!

The Final Product

Source .fla File

7 Comments

Analizame.com:

Cool. i just created another enemy that
pushes you around using the bump function.
and i think that i can create others more…
Thanks for the tutorial!


Anonymous:

when creating mcInvisMarker i dont understand what you mean by set alpha to 0.


Anonymous:

is mcEnemy suposed to be exported for actionscript?


Anonymous Answerer to Anonymous:

Alpha is the transparency setting, to turn it invisible you must have the Alpha “Color” setting to 0 (put it at 1 while your using it so you dont loose it 😉


Anonymous:

How do you make the . you take? Cause i cant find it in the tut. 🙁


bunnyphantom:

How do I add a 2nd enemy? I copied and pasted the enemy script, and changed the ‘mcEnemy’ to ‘mcEnemy1’ (Which is what my 2nd monster is called). But when I go to test the game, all there is, is the mcMain, and he won’t move or anything.


Reply- How do you make the. you take?:

I found this tutorial on tutorialsed that tells you how to do the dot thing, http://www.tutorialized.com/tutorial/Flash-CS3-tutorial-RPG-ActionScript-2-Talkable-NPC-Health-Movement-Pickup/43906

But i’m not sure how you can add the pick ups to the array


«
»