AS3 Avoider Game Tutorial, Part 2: Multiple Enemies

by Michael Williams on September 22, 2008 · 131 comments

in Avoider Game Base,Tutorial

(This tutorial is also available in Spanish, Polish, and Italian.)

Introduction

In this part of my AS3 conversion of Frozen Haddock‘s tutorial, I’ll show you how to add multiple enemies to your game. Check out what you’ll be working towards by clicking the image below.

screenshot

Setting Up

If you didn’t follow along with the first part of the tutorial, grab the zip file here and extract it to your hard drive, making sure it keeps the folder paths intact.

If you did follow the first part, I recommend making a backup of your existing game files by copying the main folder (I called mine AvoiderGame-MJW, remember) and naming the copy something like AvoiderGame-MJW-part1. This way, you can look back at the early game in the future and see how much you’ve progressed!

Either way, open the FLA file. Open the document class file (mine’s called AvoiderGame.as) and the Enemy.as file too, since we’re going to be editing them a lot.

Taking Control of the Enemy

Take another look at the Enemy’s constructor (it’s located in the Enemy.as file):

?View Code ACTIONSCRIPT3
6
7
8
9
10
		public function Enemy() 
		{
			x = 100;
			y = -15;
		}

This little bit of code means that every single instance of the Enemy class will start off at (100,-15). This will not make for a very interesting Avoider game. It’s also not going to be any use when we start making enemies that move upwards — if they start above the screen, they’ll never make it onto the playing field!

Change the constructor function like so:

?View Code ACTIONSCRIPT3
6
7
8
9
10
public function Enemy( startX:Number, startY:Number ) 
{
	x = startX;
	y = startY;
}

We’ve added parameters to the constructor function. Now when we create an instance of Enemy in the document class, we can pass x and y values to it to specify where it should appear.

Switch to your document class (that’s AvoiderGame.as) and change the code that creates a new enemy from:

?View Code ACTIONSCRIPT3
15
enemy = new Enemy();

to:

?View Code ACTIONSCRIPT3
15
enemy = new Enemy( 100, -15 );

If you run the game (Control > Test Movie, remember), you’ll see that the enemy starts at (100,-15) — which is exactly as we’d expect. We’ve passed the values 100 and -30 to the Enemy constructor, which has first set startX = 100 and startY = -15, and then set x = startX and y = startY. Of course you can change the numbers in Enemy(100,-15) to make it start out in different places. Try it out.

Irresponsible Cloning

How can we make more than one enemy appear? Let me show you an example of what not to do. It’s the simplest method: we literally copy and paste the enemy until we have as many as we need.

First, define a few more enemies as public vars in your document class (lines 10-12):

?View Code ACTIONSCRIPT3
7
8
9
10
11
12
13
14
public class AvoiderGame extends MovieClip 
{
	public var enemy:Enemy;
	public var eric:Enemy;
	public var ernie:Enemy;
	public var emily:Enemy;
	public var avatar:Avatar;
	public var gameTimer:Timer;

Next, set these up with different starting positions in the document class’s constructor, and add (using .addChild) them to the game (lines 20-25):

?View Code ACTIONSCRIPT3
16
17
18
19
20
21
22
23
24
25
public function AvoiderGame() 
{
	enemy = new Enemy( 100, -15 );
	addChild( enemy );
	eric = new Enemy( 160, -120 );
	addChild( eric );
	ernie = new Enemy( 205, -60 );
	addChild( ernie );
	emily = new Enemy( 317, -85 );
	addChild( emily );

Next, in your onTick function, get them all moving:

?View Code ACTIONSCRIPT3
37
38
39
40
41
42
public function onTick( timerEvent:TimerEvent ):void 
{
	enemy.moveDownABit();
	eric.moveDownABit();
	ernie.moveDownABit();
	emily.moveDownABit();

Finally, check each of them for a collision with the player:

?View Code ACTIONSCRIPT3
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
if ( avatar.hitTestObject( enemy ) ) 
{
	gameTimer.stop();
}			
if ( avatar.hitTestObject( eric ) ) 
{
	gameTimer.stop();
}			
if ( avatar.hitTestObject( ernie ) ) 
{
	gameTimer.stop();
}			
if ( avatar.hitTestObject( emily ) ) 
{
	gameTimer.stop();
}

Run the game. Sure, it works, but it’s a pretty very messy solution. Go ahead and undo all of that copying and pasting, it’s unnecessary.

Forming an Army

Copying and pasting enemies like that lets us refer to each one by a specific name. But… do we ever actually need to do that? Is there ever a situation where we need to tell just one enemy to act differently to the rest? Only when something happens to that enemy — when it hits the player, perhaps, or when it leaves the screen — but in those cases we single the enemy out by its situation, not by its name. I’ll show you what I mean in a bit.

What we’re going to do is create a group of enemies, and use the document class as a “general”, telling the group how to behave. I’m going to call this group army, for obvious reasons. We’ll use an Array as the structure for the group; this is basically an ordered list of objects.

First we need to define the new army, just as we did for the enemy. It’s going to have to be accessible for the duration of the game, so we’ll define it within the document class but outside the constructor (line 7):

?View Code ACTIONSCRIPT3
7
8
9
10
11
12
public class AvoiderGame extends MovieClip 
{
	public var army:Array;
	public var enemy:Enemy;
	public var avatar:Avatar;
	public var gameTimer:Timer;

Also, remove that line, public var enemy:Enemy; (line 10 above). Since the enemies are all going to be part of the army, and the army is a public var, we don’t need the enemies to be public vars any more.

Now, alter the game’s constructor like so:

?View Code ACTIONSCRIPT3
14
15
16
17
18
19
public function AvoiderGame() 
{
	army = new Array();
	var newEnemy = new Enemy( 100, -15 );
	army.push( newEnemy );
	addChild( newEnemy );

What have we done?

  • Line 16 — Set the army as a new instance of Array, just as we’ve done for the enemy.
  • Line 17 — So as not to get confused, I’ve renamed enemy to newEnemy, as it will now only be used for creating new enemies. Since there’s no var newEnemy statement (public or otherwise) anywhere, we need to stick var on the front here.
  • Line 18 — Added the new enemy to the army. (“Pushed” it onto the end of the list of enemies.)
  • Line 19 — added the new enemy to the game (all that’s changed here is that enemy has been renamed newEnemy).

Now we need to make the enemy move. Remember this is done in the onTick function. We’re going to need a for each…in loop, which is new to ActionScript 3.

Edit the onTick function so that:

?View Code ACTIONSCRIPT3
31
32
33
34
35
public function onTick( timerEvent:TimerEvent ):void 
{
	enemy.moveDownABit();
	avatar.x = mouseX;
	avatar.y = mouseY;

becomes:

?View Code ACTIONSCRIPT3
31
32
33
34
35
36
37
38
public function onTick( timerEvent:TimerEvent ):void 
{
	for each ( var enemy:Enemy in army ) 
	{
		enemy.moveDownABit();
	}
	avatar.x = mouseX;
	avatar.y = mouseY;

I think it’s actually quite clear what’s happening here. The for each…in loop goes through everything in the army’s list of items and tells it to move down a bit. The confusing part is that it looks like we are telling the loop only to look at items of type Enemy — but that’s not how it works. We are merely telling the loop that it should expect everything inside the army to be of type Enemy. We could have added the avatar to the army back in the constructor function — and then the game would have crashed because the Avatar class doesn’t contain a function called moveDownABit(). Anyway, don’t worry about this too much, it’ll become more intuitive as you use it more.

There’s one more change we need to make before we can run this: checking for collisions. We can just move the collision detection code to be inside the for each…in loop (lines 36-39):

?View Code ACTIONSCRIPT3
31
32
33
34
35
36
37
38
39
40
41
42
43
public function onTick( timerEvent:TimerEvent ):void 
{
	for each ( var enemy:Enemy in army ) 
	{
		enemy.moveDownABit();
		if ( avatar.hitTestObject( enemy ) ) 
		{
			gameTimer.stop();
		}
	}
	avatar.x = mouseX;
	avatar.y = mouseY;
}

Although, this means that we are checking for collisions before we’ve moved the avatar, i.e. we’re checking if the enemy is colliding with where the player used to be, which hardly seems fair. So, rearrange it a bit:

?View Code ACTIONSCRIPT3
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public function onTick( timerEvent:TimerEvent ):void 
{
	avatar.x = mouseX;
	avatar.y = mouseY;
 
	for each ( var enemy:Enemy in army ) 
	{
		enemy.moveDownABit();
		if ( avatar.hitTestObject( enemy ) ) 
		{
			gameTimer.stop();
		}
	}
}

Save it and run it, and you’ll see everything’s working out just as before. Now to add more enemies.

Adding to the Army

Try this:

?View Code ACTIONSCRIPT3
31
32
33
34
35
public function onTick( timerEvent:TimerEvent ):void 
{
	var newEnemy:Enemy = new Enemy( 100, -15 );
	army.push( newEnemy );
	addChild( newEnemy );

You should recognise lines 33-35. We’ve just put the code for creating a new enemy in the onTick, so that every tick it will create a new enemy. What do you expect will happen when we run it?

screenshot

Hm. It’s creating new enemies in the same place at such a fast rate that they’re overlapping. Let’s try to fix this by starting the new enemies off in all different places.

We’ll need the Math.random() function for this. Math.random() returns a random number between 0 and 1. My game is 400 pixels wide, so I’d like the enemies to start off with an x-coordinate of between 0 and 400. Therefore (lines 33-34):

?View Code ACTIONSCRIPT3
31
32
33
34
35
36
public function onTick( timerEvent:TimerEvent ):void 
{
	var randomX:Number = Math.random() * 400;
	var newEnemy:Enemy = new Enemy( randomX, -15 );
	army.push( newEnemy );
	addChild( newEnemy );

So now the enemies will be created at some random point above the screen. Save it and run it, and what do we get?

screenshot

Argh!

All right, so, maybe they’re still coming in at too fast a rate…

Less Enemies per Second, Please

Right now, the enemies are appearing at a rate of one new enemy per tick. Since a tick is 25ms, there are 40 ticks per second, and therefore 40 new enemies per second. I think if we reduced this to four new enemies per second, we’d be good. Since 4 is 1/10 of 40, we need to reduce the new-enemies-per-tick rate to 1/10.

We could do this by making a new enemy appear every ten ticks. I think it would be more fun if we said there was a 1/10 chance of a new enemy appearing on any given tick; this way, sometimes we’d have more than four enemies appearing per second, and sometimes we’d have less, but over time we’d average out at four/sec. The uncertainty involved makes the game a little more exciting, however (though perhaps “exciting” is a little bit of a misnomer at this early stage of the game’s development…).

How can we do this, then? Well, we know that Math.random() generates a random number between 0 and 1. Since these random numbers are evenly distributed, that means that there’s a 1/10 chance of the number generated being between 0 and… well, 1/10. I.e., the chance of Math.random() < 0.1 is 1/10.

So, let’s change our code to make use of this fact (lines 29 & 34):

?View Code ACTIONSCRIPT3
28
29
30
31
32
33
34
35
36
public function onTick( timerEvent:TimerEvent ):void 
{
	if ( Math.random() < 0.1 )
	{
		var randomX:Number = Math.random() * 400;
		var newEnemy:Enemy = new Enemy( randomX, -15 );
		army.push( newEnemy );
		addChild( newEnemy );
	}

(Note: each time you call Math.random() the number generated is different, so this new code isn’t interfering with our random positioning code.)

Save it and run it.

screenshot

Much better! Actually, I think this may now be too easy. Why not fiddle around with the if (Math.random() < 0.1) statement to see what works best for you?

Wrapping Up

That’s it for part two. You can download a zip file with everything I’ve been working on from here.

In the next part, we’ll add a Game Over screen so that finally the game will have an aim! It’s available here.

If you’d like to try expanding upon this part yourself, how about trying to make the starting enemy appear at a random location? I’m sure you can do that now. Also, the enemies sometimes appear partly off one side of the screen. Can you fix that?

{ 129 comments… read them below or add one }

ayumilove May 17, 2010 at 1:11 am

@vllmstl

just try that code , if it works, than its your software problem.
else check the code above it and see whether there are any syntax errors.

vllmstl May 17, 2010 at 2:00 pm

@ayumilove

Thanks for the help, will try it out now and let you know.

:) )

vllmstl May 17, 2010 at 2:07 pm

Hi Michael Williams,

I dont know, i am new to as3 and came across that code for the first time.
and it neither turned blue nor worked for me, Just the “for ‘each’ statement”
let me try to reinstall my flash cs3… may be that might help me.
Thanking you,
vllmstl

vllmstl May 17, 2010 at 2:10 pm

hi Michael Williams,

I forgot to thank you for the awesome tutorial.

hehehe :)

ayumilove May 17, 2010 at 3:49 pm

i tried on my adobe flash,
the “for” turns into blue when i typed “for each”
the each does not turn into blue.

Take note that the syntax coloring in this actionscript code within this blog is not necessarily colors the same in adobe flash.

ProfeaX May 24, 2010 at 6:19 pm

I’m getting error while changing enemy = newEnemy (); to enemy = new Enemy( 100, -15 );
1137: Incorrect number of arguments. Expected no more than 0.

ayumilove May 24, 2010 at 11:23 pm

@Profeax
The new Enemy instance that you are trying to create does not expect any arguments in its constructor

check your Enemy.as class
and you find that your contructor would be like this

public function Enemy ()
{
}

As you can see, the constructor expects zero/none arguments within the round brackets ()

1 solution

To solve this problem, when creating a new instance, do not input the Enemy coordinates 100,-15

2 solution

Have your constructor to take in 2 arguments
public function Enemy (posX : Number, posY : Number)
{
this.x= posX ;
this.x= posY
}

ayumilove May 24, 2010 at 11:25 pm

correction ….

public class Enemy extends Sprite {
public function Enemy (positionX : Number, positionY : Number)
{
x= positionX ;
y= positionY ;
}
}

Michael Williams May 29, 2010 at 12:15 am

@vllmstl: Oh! Ayumilove is right, each is not supposed to turn blue. Silly me.

@Profeax: As usual, ayumilove’s hit the nail on the head :) Check the second code box in “Taking Control of the Enemy”.

ayumilove May 29, 2010 at 12:38 am

Is there any edit button where i can edit my comment if I had made a mistake?

Michael Williams May 29, 2010 at 12:46 am

There used to be… I wonder where it went. Seems it’s still set up to do that; I guess the plugin isn’t compatible with my blog software any more :/
With a bit of luck it’ll be fixed automatically at some point.

Samir Uddin June 5, 2010 at 1:33 pm

thx dude this really helped me out a lot. Although sumtimes after ive written the code and test it it brings back various errors witch i cant seem to solve. BUT, when i rewrite the code out EXACTLY THE SAME as it was before the errors dissapear! WIERD! lol

Michael Williams June 12, 2010 at 1:54 pm

Cheers, Samir :) Haha, yeah, that seems to happen a lot. Mysterious.

ayumilove June 13, 2010 at 1:03 am

@Samir Uddin

To learn from mistake, you need to backup a copy of the class that has the problem, and compare with that class to the rewritten class. From there, you know exactly what causes the error and you can avoid them in the future. If you were to make a game, and this thing happens again, where would you copy the code from? There won’t be any code lying out there for you to copy/rewrite.

Xadjim June 15, 2010 at 6:13 am

Wow man nice tutorial…. i tried to play a little bit with the math.random function and i build in a difficulty with this:

        difficulty+=0.00001
        if (Math.random()<difficulty)
        {
                     blablabla
                    }

the only thing i still dont realy get is the “gameTimer.addEventListener(TimerEvent.TIMER, onTick);” stuff

Dean June 19, 2010 at 6:41 pm

I have received the following error:
TypeError: Error #1034: Type Coercion failed: cannot convert 100 to Enemy.
at AvoiderGame/onTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

The code for the affected is below:

public function onTick( timerEvent:TimerEvent ):void
        {
            avatar.x = mouseX;
            avatar.y = mouseY;

        for each ( var enemy:Enemy in army )
        {
            enemy.moveDownABit();
            if ( avatar.hitTestObject( enemy ) )
            {
            gameTimer.stop();
            }
        }

The error seems to be occuring after the ‘for each…in’ and before the ‘enemy.moveDownABit()’ line of code.
Any help will be greatly appreciated.
I am using Flash Pro CS4.

Michael Williams June 22, 2010 at 3:17 pm

@Xadjim: Cheers :) Nice — what do you put in the blablabla bit?

@Dean: Odd error. Looks like your army array contains the number 100.

Before that “for each”, call:

trace( army.toString() );

That’ll let you see if there’s anything unusual in there. If there is, check your code to see if you can figure out where it’s being added.

Callum June 26, 2010 at 11:56 am

HI there, excellent tutorial!

I am having a problem with Math.random. I am trying to reduce the number of enemies, but no matter what value I enter to replace your <0.1, the game still works but the number of enemies never changes.

ayumilove June 27, 2010 at 3:53 am

@Callum

If you would like to reduce the number of enemies that can appear on the screen at any point of time, you should do an if-else statements on the array length that stores those enemies!

A sample code :
[code]
if ( Math.random() < 0.1 && army.length < 10) { /* spawn enemy */ }
[/code]

ayumilove June 27, 2010 at 3:56 am

You can set a constant value of 10 (as shown below) to limit the enemies that can be on screen.

if ( Math.random() < 0.1 && army.length < 10) { /* spawn enemy */ }

The previous method that you used, (changing the value of 0.1 in Math.random)
simply increases/decreases the probability of the enemy spawn. The bigger the number (closing in to 1) has higher chance to spawn an enemy compare to the lower number (closing in to zero)

Jake June 29, 2010 at 2:59 am

Okay, I can get everything working until the end. Whenever I do

if ( Math.random() < 0.1 )

part I get tons of lag. If I bring it to say, 10, it doesn’t at all. The time that they’re going down doesn’t change at all, it seems to basiclly only change lag or no lag. Here’s my code…

package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class AvoiderGame extends MovieClip
{
    public var army:Array
    public var avatar:Avatar;
    public var gameTimer:Timer;
    public function AvoiderGame()
    {
        army = new Array();
        var newEnemy = new Enemy(100, -15);
        army.push( newEnemy );
        addChild( newEnemy );
        avatar = new Avatar();
        addChild( avatar );
        avatar.x = mouseX;
        avatar.y = mouseY;
        gameTimer = new Timer( 25 );
        gameTimer.addEventListener( TimerEvent.TIMER, onTick );
        gameTimer.start();
    }

public function onTick( timerEvent:TimerEvent ):void

{ if (Math.random() < 0.1 )
{
var randomX:Number = Math.random() * 600;
var newEnemy:Enemy = new Enemy( randomX, -15 );
army.push( newEnemy );
addChild( newEnemy );
for each ( var enemy:Enemy in army )

    {
    enemy.moveDownABit();
    if ( avatar.hitTestObject( enemy ) )
    { gameTimer.stop();

    }

}

avatar.x = mouseX;
avatar.y = mouseY
}

}

}
}

any help would be appreciated, thanks!

Jake

ayumilove June 29, 2010 at 5:23 am

@Jake
sorry, I don’t understand what’s your question about, so I can’t really help you.
Probability of enemy spawning is only from the range of zero to one. Not ten (10).

I don’t see any errors in your code, should be fine.

Jake June 29, 2010 at 10:22 pm

@ayumilove
When I put it at 0.1 or less, it lags a ton. The higher the number it is, the less the lag is, but the enemies per second is the same. Know what’s up?

Thanks!

ayumilove June 30, 2010 at 12:51 am

Don’t know, send me your .fla to my email so I could check it out :)
email : ayumiyoutube(at)gmail(d0t)com

Michael Williams July 5, 2010 at 7:48 pm

@Callum: Not sure what you mean… did ayumilove’s comment about army.length help?

@Jake: That is weird :S Higher number should cause higher lag… maybe your curly braces are in the wrong place? If ayumilove’s generous offer of help doesn’t solve it, post again :)

ayumilove July 6, 2010 at 3:15 am

I did not receive his .fla and .as files
So I think he has a way to manage that problem ;)

Michael Williams July 7, 2010 at 6:55 pm

Let’s hope so :)

PlayHermit August 1, 2010 at 8:04 pm

I hope you’re still reading these comments MJW. I tried to get the little end activity and I figured out how to get the starting enemy to appear at random places, but I could never figure out how to keep them from only spawning within the box and not appear off the screen.

I thought just modifying the x value would do the trick but I guess theres more to it, right?

var randomX:Number = Math.random() * 370;
var newEnemy:Enemy = new Enemy( randomX, -15 );

As you can see, I attempted to fix the problem by lowering the the number in the first line from 400 to 370 but they’re still popping up a little off the screen. =(

PlayHermit´s last blog ..Gameplay – Pokemon Stadium 2- Defeating BugsyMy ComLuv Profile

Michael Williams August 21, 2010 at 12:27 am

Hey PlayHermit. I’m reading them, just a little behind on them — sorry!

Think about it this way: since the lowest value that Math.random() can be is 0, the lowest value that absoluteX can be using your code above is (0 * 370) = 0. But you need the lowest value to be, what, 15? 25? (I don’t know the width of your avatar.)

Hope that hint helps :)

Leave a Comment

Writing code? Write <pre> at the start and </pre> at the end to keep it looking neat.

CommentLuv Enabled

Anti-Spam Protection by WP-SpamFree

{ 2 trackbacks }

Previous post:

Next post: