Tutorials
Tutorials >> Avoider
Posted by fflash
PART 1
The game we shall be making is shown below. You are the black circle, try and get the blue squares but avoider the white, 'enemy' circles. Here is a live example of what we will create.

During this project, we will be building on the skills learned from our previous tutorials, such as Pong!. Specifically, we will also be adding the following to our ActionScripting arsenal:
- Using external classes and initialising them as objects
- Using these objects to hold information about themselves
- Use the mouse to move
- Removing an object fully from AS3 (harder than it sounds)
Well, let's crack on. First of all, you will need to create a new project, including one .fla and one .as. Call the .as file Avoider.as (it is customary to entitle classes, and thus their .as files, with a capital letter). Save them both in the same folder. Then, in the .fla, make the document class "Avoider" (with the capital letter, see for more one of our previous tutorials). This will make sure that the Avoider class constructor is loaded as soon as the swf is compiled. Good.
Now we need to actually put some code in Avoider.as. Go to it, and type:
While you're in you .fla, you'll need to add in the four walls. Make them quite thin, at most about 15px (or alter the later code). Give them, in clockwise order, the instance names walltop, wallright, wallbottom and wallleft. WE will be using them later for both the player and the enemies, who 'bounce' off them.
Finally for today, then, we will be adding in some variables to use later. These need to go into between the Avoider class definition line and the Avoider function definition line, and are as follows:
All this is to come of course, but I do so hope you will join me for it.
PART 2
A copy of the full source code for today's tutorial is available.
The game we shall be making is shown below. You are the black circle, try and get the blue squares but avoider the white, 'enemy' circles.Here is a live example of what we will create.

Well, that's the slight rewrite of what I said last week out of the way (oh, the beauties of copy and paste!). This week we are going to be actually doing something. Sharp intake of breath. From last week, you should have four Movie Clips for the walls, with which we shall be interacting either this week or next. For now though, we will turn our attention to the player.
Go to your ActionScript, and inside the constructor - the Avoider() function - call a new function, which we are about to create, called drawPlayer(). In case you don't know how to do that (and you really should by now), it's like this:
Go back to the constructor function and, underneath the drawPlayer function, add in this line:

And here is that one, which handles all negative Y differences:
PART 3
A copy of the full source code for today's tutorial is available.
The game we shall be making is shown below. You are the black circle, try and get the blue squares but avoider the white, 'enemy' circles.Here is a live example of what we will create.

If you remember back to last time, I said that we were going to do targets and scoring today, and that we shall. Naturally, we shall be starting with the former - the targets. These are essentially little blue squares drawn in. Alright - they are little blue squares drawn in.
First of all, then, we're going to make sure a target gets drawnin at the beginning, by adding this line into the constructor:
And, thankfully, that's it. Yes, I know there's been a lot of code today, but we've achieved something at least: if you now compile your game (CTRL/Command + Enter) you should see that the player moves towards the mouse, and when it touches the target you get some points.
PART 4
A copy of the full source code is available. The code for the extra, enemy class is here, don't forget that either.
The game we shall be making is shown below. You are the black circle, try and get the blue squares but avoider the white, 'enemy' circles.Here is a live example of what we will create.

Now, unfortunately, is the moment when we must focus on the hardest part of this games, in my view. Th part to which I am referring is the enemies themselves. Although they have simple movement, it will be a steep learning curve for anyone following (only) these tutorials. That's why I've broken it down for you, into a few little subheadings.
Why is it going to be so difficult?
It is going to be just a bit tricky because for the first time we shall have to introduce a second external class (.as file), import it and use it.
Why?
Here goes: each enemy has a direction - either horizontal, or vertical. Within these it either goes left or right. Now imagine you're a computer and you pick an enemy at random. How are you going to know whether to move it right or left or up or down? Okay, you could set a complicated set of event listeners which switched between one another, but there's a (comparitively) easy way: use a class for the enemy, which hold whether it's a horizontal enemy (from now one: H-Enemy) or vertical one (V-Enemy)
First things first: how do we create the enemy and draw it?
Ah, just like we did with the target and the player. Create a new function-call within the constructor (avoider()) function and call it drawNewEnemy(). NOTE: make sure the function calls are in this order: player, target, enemy! Then it's basically a cut and paste job from the target, information about which you can find if you look at the previous tutorial:
I think actually.. yes, we'll do the touchPlayer function in a minute. First though, we need to create that extra class.
What information are we going to hold in our extra class?
Well, the thing we have to hold in the current direction of movement, which we'll call "mydirection". We'll also keep a backup variable for disabling the enemy quickly, which will be "hastouched". For more information about variables, see this tutorial. For now though, create a new .as file and save it as Enemy.as (with a capital E, as all classes should begin with a capital). That should tell you that we're calling our class 'Enemy', if you couldn't remember from earlier. Here is all the code you need to put in that:
Moving the enemy around
We're one the home stretch now - all we have left to do is attach the event listeners for move left and right, utilising the mydirection variable we created earlier. We're going to do this right after the enemy is created, while we can de facto affect all enemies. Go back to the drawNewEnemy() function then, and add, to the end:
And, thankfully, that's it. I mean, that's it. Done. Finished. Finito. Whatever will I write a tutorial about next week? If you now compile your game (CTRL/Command + Enter) you should see that it all works.
PART 1
The game we shall be making is shown below. You are the black circle, try and get the blue squares but avoider the white, 'enemy' circles. Here is a live example of what we will create.

During this project, we will be building on the skills learned from our previous tutorials, such as Pong!. Specifically, we will also be adding the following to our ActionScripting arsenal:
- Using external classes and initialising them as objects
- Using these objects to hold information about themselves
- Use the mouse to move
- Removing an object fully from AS3 (harder than it sounds)
Well, let's crack on. First of all, you will need to create a new project, including one .fla and one .as. Call the .as file Avoider.as (it is customary to entitle classes, and thus their .as files, with a capital letter). Save them both in the same folder. Then, in the .fla, make the document class "Avoider" (with the capital letter, see for more one of our previous tutorials). This will make sure that the Avoider class constructor is loaded as soon as the swf is compiled. Good.
Now we need to actually put some code in Avoider.as. Go to it, and type:
- Code: Select all
package{
import flash.display.MovieClip;
public class Avoider extends MovieClip{
public function Avoider(){
}
}
}
- Code: Select all
import flash.text.TextField;
import flash.display.Shape;
import flash.events.Event;
While you're in you .fla, you'll need to add in the four walls. Make them quite thin, at most about 15px (or alter the later code). Give them, in clockwise order, the instance names walltop, wallright, wallbottom and wallleft. WE will be using them later for both the player and the enemies, who 'bounce' off them.
Finally for today, then, we will be adding in some variables to use later. These need to go into between the Avoider class definition line and the Avoider function definition line, and are as follows:
- Code: Select all
public var target:MovieClip = new MovieClip;
public var player:MovieClip = new MovieClip;
public var score:int = 0;
public var mouseDiffX:int = 0;
public var mouseDiffY:int = 0;
All this is to come of course, but I do so hope you will join me for it.
PART 2
A copy of the full source code for today's tutorial is available.
The game we shall be making is shown below. You are the black circle, try and get the blue squares but avoider the white, 'enemy' circles.Here is a live example of what we will create.

Well, that's the slight rewrite of what I said last week out of the way (oh, the beauties of copy and paste!). This week we are going to be actually doing something. Sharp intake of breath. From last week, you should have four Movie Clips for the walls, with which we shall be interacting either this week or next. For now though, we will turn our attention to the player.
Go to your ActionScript, and inside the constructor - the Avoider() function - call a new function, which we are about to create, called drawPlayer(). In case you don't know how to do that (and you really should by now), it's like this:
- Code: Select all
drawPlayer();
- Code: Select all
public function drawPlayer() {
}
- Code: Select all
player.graphics.beginFill(0x000000);
player.graphics.drawCircle(0, 0, 10)
; player.x = 200;
player.y = 200;
player.graphics.endFill();
addChild(player);
Go back to the constructor function and, underneath the drawPlayer function, add in this line:
- Code: Select all
addEventListener(Event.ENTER_FRAME, mouseMovement);
- Code: Select all
public function mouseMovement(event:Event):void {
mouseDiffY = mouseY - player.y;
mouseDiffX = mouseX - player.x;
}

And here is that one, which handles all negative Y differences:
- Code: Select all
if (mouseDiffY < 0) {
if (!player.hitTestObject(walltop)) {
player.y += mouseDiffY / 6;
}
}
- Code: Select all
if (mouseDiffY > 0) {
if (!player.hitTestObject(wallbottom)) {
player.y += mouseDiffY / 6;
}
}
if (mouseDiffX < 0) {
if (!player.hitTestObject(wallleft)) {
player.x += mouseDiffX / 6;
}
}
if (mouseDiffX > 0) {
if (!player.hitTestObject(wallright)) {
player.x += mouseDiffX / 6;
}
}
PART 3
A copy of the full source code for today's tutorial is available.
The game we shall be making is shown below. You are the black circle, try and get the blue squares but avoider the white, 'enemy' circles.Here is a live example of what we will create.

If you remember back to last time, I said that we were going to do targets and scoring today, and that we shall. Naturally, we shall be starting with the former - the targets. These are essentially little blue squares drawn in. Alright - they are little blue squares drawn in.
First of all, then, we're going to make sure a target gets drawnin at the beginning, by adding this line into the constructor:
- Code: Select all
drawNewTarget();
- Code: Select all
public function drawNewTarget() {
var posX:uint = randRange(30,520);
var posY:uint = randRange(30,370);
}
- Code: Select all
private function randRange(start:Number,end:Number):Number {
return Math.floor(start + Math.random() * end - start);
}
- Code: Select all
target.graphics.beginFill(0x0000FF);
target.graphics.drawRect(0, 0, 20, 20);
target.x = posX;
target.y = posY;
target.graphics.endFill();
addChild(target);
- Code: Select all
target.addEventListener(Event.ENTER_FRAME,gotToTarget);
- Code: Select all
private function gotToTarget(e:Event) {
if (e.target.hitTestObject(player)) {
e.target.x = randRange(30,520);
e.target.y = randRange(30,370);
//drawNewEnemy();
score++;
scoretext.text = "Score: " + String((score * 10));
}
}
And, thankfully, that's it. Yes, I know there's been a lot of code today, but we've achieved something at least: if you now compile your game (CTRL/Command + Enter) you should see that the player moves towards the mouse, and when it touches the target you get some points.
PART 4
A copy of the full source code is available. The code for the extra, enemy class is here, don't forget that either.
The game we shall be making is shown below. You are the black circle, try and get the blue squares but avoider the white, 'enemy' circles.Here is a live example of what we will create.

Now, unfortunately, is the moment when we must focus on the hardest part of this games, in my view. Th part to which I am referring is the enemies themselves. Although they have simple movement, it will be a steep learning curve for anyone following (only) these tutorials. That's why I've broken it down for you, into a few little subheadings.
Why is it going to be so difficult?
It is going to be just a bit tricky because for the first time we shall have to introduce a second external class (.as file), import it and use it.
Why?
Here goes: each enemy has a direction - either horizontal, or vertical. Within these it either goes left or right. Now imagine you're a computer and you pick an enemy at random. How are you going to know whether to move it right or left or up or down? Okay, you could set a complicated set of event listeners which switched between one another, but there's a (comparitively) easy way: use a class for the enemy, which hold whether it's a horizontal enemy (from now one: H-Enemy) or vertical one (V-Enemy)
First things first: how do we create the enemy and draw it?
Ah, just like we did with the target and the player. Create a new function-call within the constructor (avoider()) function and call it drawNewEnemy(). NOTE: make sure the function calls are in this order: player, target, enemy! Then it's basically a cut and paste job from the target, information about which you can find if you look at the previous tutorial:
- Code: Select all
public function drawNewEnemy() {
var posX:uint = randRange(50,500);
var posY:uint = randRange(50,350);
var anenemy:Enemy = new Enemy;
anenemy.graphics.beginFill(0xFFFFFF);
anenemy.graphics.lineStyle(2,0x000000);
anenemy.graphics.drawCircle(10, 10, 10);
anenemy.x = posX;
anenemy.y = posY;
anenemy.graphics.endFill();
addChild(anenemy);
anenemy.addEventListener(Event.ENTER_FRAME,touchPlayer);
}
I think actually.. yes, we'll do the touchPlayer function in a minute. First though, we need to create that extra class.
What information are we going to hold in our extra class?
Well, the thing we have to hold in the current direction of movement, which we'll call "mydirection". We'll also keep a backup variable for disabling the enemy quickly, which will be "hastouched". For more information about variables, see this tutorial. For now though, create a new .as file and save it as Enemy.as (with a capital E, as all classes should begin with a capital). That should tell you that we're calling our class 'Enemy', if you couldn't remember from earlier. Here is all the code you need to put in that:
- Code: Select all
package{
import flash.display.MovieClip;
public class Enemy extends MovieClip{
public var mydirection:uint = 0;//0 = down/right, 1 = up/left
public var hastouched:Boolean = false;
}
}
- Code: Select all
private function touchPlayer(e:Event) {
if (e.target.hitTestObject(player) && !e.target.hastouched) {
e.target.hastouched = true;
for (var i = (numChildren - 1); i > 5; i--) {
getChildAt(i).removeEventListener(Event.ENTER_FRAME,touchPlayer);
removeChildAt(i);
}
e.target.removeEventListener(Event.ENTER_FRAME,touchPlayer);
score = 0;
scoretext.text = "Score: 0";
drawNewTarget();
drawNewEnemy();
}
}
Moving the enemy around
We're one the home stretch now - all we have left to do is attach the event listeners for move left and right, utilising the mydirection variable we created earlier. We're going to do this right after the enemy is created, while we can de facto affect all enemies. Go back to the drawNewEnemy() function then, and add, to the end:
- Code: Select all
if (randRange(0,100) > 50) {
anenemy.addEventListener(Event.ENTER_FRAME,moveVertical);
} else {
anenemy.addEventListener(Event.ENTER_FRAME,moveHorizontal);
}
- Code: Select all
private function moveVertical(e:Event) {
if (e.target.mydirection == 0) {
e.target.y += 4;
} else if (e.target.mydirection == 1) {
e.target.y -=4;
}
if (e.target.hitTestObject(wallbottom)) {
e.target.mydirection = 1;
}
if (e.target.hitTestObject(walltop)) {
e.target.mydirection = 0;
}
}
private function moveHorizontal(e:Event) {
if (e.target.mydirection == 0) {
e.target.x += 4;
} else if (e.target.mydirection == 1) {
e.target.x -=4;
}
if (e.target.hitTestObject(wallright)) {
e.target.mydirection = 1;
}
if (e.target.hitTestObject(wallleft)) {
e.target.mydirection = 0;
}
}
And, thankfully, that's it. I mean, that's it. Done. Finished. Finito. Whatever will I write a tutorial about next week? If you now compile your game (CTRL/Command + Enter) you should see that it all works.

Twitter