Intro
Very often we see people wishing to learn AS asking how to create enemies randomly come out from the side of the screen (or top or bottom) and how to make them move. Well, in this tutorial you will learn different methods of how to do this, and hopefully other people can reply to this thread with their own ideas.
Method 1: Duplicating a movie clip
I personally wouldn't use this method, but it's nice and basic for beginners.
Firstly, create a movie clip and give it an instance name of "enemy" (minus quote marks). This will be your enemy (duh). In the actions of this MC, copy and paste the following script, then read it along so you know what's going on:
onClipEvent (load) {
// Makes sure the following only happens when the MC is loaded
this._x = Stage.width+this._width;
// sets the x position of the MC to be just to the right of screen
this._y = random(Stage.height);
// sets the y position of the MC to make it random.
// the function random(x) randomly picks a number
// from 0 to x, NOT including x. So, this line sets the
// y position to a number between 0 and the height of the stage.
speed = (Math.random()+1)*5;
// sets a variable "speed" which will determine how fast the
// enemy will go. the Math.random() function randomly picks a
// number between 0 and 1. so it could be 0, 0.48826, 0.5 or 1.
// we add the +1 at the end of this to ensure the speed isn't 0.
// we also add the *5 to multiply the number by five, so now we
// get a random number between 1 and 5
}
onClipEvent (enterFrame) {
// Makes the following code occur as each each frame passes.
if (this._x<0) {
// tests to see if the x position of the MC is off screen
this.removeMovieClip();
// if it is, it removes it.
}
this._x -= speed;
// every frame, the x position of the MC moves left (decreases)
// at a rate of x amount of pixels every frame, where x represents
// the variable we set earlier, "speed". This makes it so that each
// enemy has a different speed.
}
Then add this script to the frame your MC is on, then read it for information on what's happening:
onLoad = function () {
// Makes sure the following only happens when the MC is loaded
i = 0;
// sets the variable "i" to 0. "i" will act as a counter
};
onEnterFrame = function () {
// makes the following code occur as each frame passes.
i++;
// adds 1 on to "i"
if (random(50) == 0) {
// again, the random function selects a number in this
// case between 0 and 49 inclusive. Lower fifty for more enemies. This sign "=="
// means equals, so in English the line says "If a random number
// between 0 and 49 inclusive is 0, then perform the
// following script". In other words, each frame we have
// a one in fifty chance of the next line occuring.
duplicateMovieClip(_root.enemy, "enemy"+i, i);
// this copies the MC that is on the stage (which you
// hopefully gave it an instance name of "enemy"). It
// gives the copy a name of "enemy" followed by whatever
// the variable "i" is, which is why I said it acts like a
// counter. The "i" at the end sets the depth of the MC
// to "i" which means that every copy will be on top of
// the previously copied MCs
}
};
The code explains everything.
Method 2: Taking a movie clip from the library
I always use this method because I don't have to already have a MC on the stage, rather I can simply take it from the library and tell it what to do all in one frame.
Firstly, creating an enemy MC, and in the library, right click on it, select "Linkage", then "Export for AS" and in the identifier type "enemy" (minus quotes). This allows us to select it from the library using AS.
Then paste these actions into the frame, and read through it:
onLoad = function () {
i = 0;
};
onEnterFrame = function () {
i++;
if (random(50) == 0) {
// you already know about the above
var enemy:MovieClip = attachMovie("enemy", "enemy"+i, i);
// this creates a variable (in this case a movie clip)
// whose name is enemy. The attachMovie finds an MC from
// the library (we named it "enemy"), and gives it an
// original name and depth.
// You should now know what the rest of the code means.
enemy._x = Stage.width+enemy._width;
enemy._y = random(Stage.height);
enemy.onEnterFrame = function() {
if (this._x<0) {
this.removeMovieClip();
}
this._x -= 5;
// I don't yet know how to set a random speed so
// maybe someone else can post that.
};
}
};
Again, can someone else post up how to make a random speed in the second method, I just do it by setting it inside the movie clip.
Hope you've learnt something and if anyone else can post their code it would be great.
Thanks,
Kabomb.