00:00
00:00

## AS: Random Enemies

7,009 Views | 32 Replies

AS: Random Enemies 2006-04-22 02:04:16

AS: Main

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:

// 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:
// 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:
i = 0;
};
onEnterFrame = function () {
i++;
if (random(50) == 0) {
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.

Response to AS: Random Enemies 2006-04-22 04:20:53

Great tutorial in depth! good,
ill try it when i need it im sure it will be usefull!

Response to AS: Random Enemies 2006-04-22 05:04:15

Good job. You explained it well and it has lots of variations. This will be added to AS:Main in a week or so.

Response to AS: Random Enemies 2006-04-22 05:32:34

Nice.Looks like alot of code

Get Fuckle'd

Response to AS: Random Enemies 2006-04-22 05:55:56

cool tutorial rather basic but explained well..

Response to AS: Random Enemies 2006-04-22 06:01:37

Great Tutorial!

Im checking if it works...

Response to AS: Random Enemies 2006-04-22 06:03:07

This'll be useful! Kudos! :D

Response to AS: Random Enemies 2006-04-22 18:26:32

hi i have a problem hopefully someone can solve

im duplicating clips using the first method, where enemy soldiers walk onto the stage. They all die when a bullet hits them, except for the movieclip that is duplicated to start off with. can anyone help me out?

thanks

Response to AS: Random Enemies 2006-04-22 19:37:07

^^^^^ nevermind. i figured it out.

if anyone else has the same problem- its because the depth of the original movieclip is in the negatives and you have to change that.

Response to AS: Random Enemies 2006-04-22 19:55:10

Great tut, I loved how you explained every step.

Take that, Copy&Pasters!

wew

Response to AS: Random Enemies 2006-08-02 00:32:23

sucks ass. code will not work

Response to AS: Random Enemies 2006-08-02 03:12:07

Awesome tutorial man! I'm pretty new to scripting and I managed to make this-

http://img204.images..?image=savengkz9.swf

The score and health totally bug out though, what's up with that!1? lol

Response to AS: Random Enemies 2006-09-10 03:27:19

Help. I've given help to my enemy's and when I click it, it goes down 20 and that works and when it reaches 0 it gets removed well that works and when it hit's the castle your health goes down 10 and the movieclip is removed but whenever you do that to the original it doesn't work. Here's my code:

onClipEvent(enterFrame){
if(this.hitTest(_root.castle)){
this.removeMovieClip();
}
if(this.hitTest(_root.castle)){
_root.health-=10
}
if(_root.health==0){
_root.gotoAndStop(2)
}
}
on(press){
enemyhealth-=20
}
onClipEvent(enterFrame){
if(enemyhealth==0){
this.removeMovieClip();
}
}

Response to AS: Random Enemies 2006-09-10 03:34:03

At 9/10/06 03:27 AM, Hornby wrote: Help. I've given help to my enemy's and when I click it, it goes down 20 and that works and when it reaches 0 it gets removed well that works and when it hit's the castle your health goes down 10 and the movieclip is removed but whenever you do that to the original it doesn't work. Here's my code:

onClipEvent(enterFrame){
if(this.hitTest(_root.castle)){
this.removeMovieClip();
}
if(this.hitTest(_root.castle)){
_root.health-=10
}
if(_root.health==0){
_root.gotoAndStop(2)
}
}
on(press){
enemyhealth-=20
}
onClipEvent(enterFrame){
if(enemyhealth==0){
this.removeMovieClip();
}
}

Well if the enemy isn't hitting the castle everything works well. As soon as it hits the castle the enemy is removed and then nothing further executes. In additions, if it finished executing you would be testing to see if an undefined mc was hitting the castle. So you should try combineing the two to make it something like:

if (this.hitTest(_root.castle)) {
_root.health-=10;
removeMovieClip(this);
}

Instead of the two seperate hitTests.

Response to AS: Random Enemies 2006-09-10 03:37:49

you don't understand. It works the script, but when the original hit's the castle it doesn't remove butt he health goes down 10 and then another 10 and so on because it's not being removed and when his health reaches 0 on the original it just keeps moving toward the castle and the health goes into the minus.

Response to AS: Random Enemies 2006-09-10 03:42:08

Sorry about the double post but here's an example.

Response to AS: Random Enemies 2006-09-10 04:04:54

At 9/10/06 03:42 AM, Hornby wrote: Sorry about the double post but here's an example.

And you're right, I found it easier to just glance over the code than try to decipher your english.

Now that I'm done being an e-asshole, I'll try to help.

removeMovieClip can't remove anything on a negative depth. Anything you put on the stage in the Flash authoring environment automatically gets a negative depth. You can change that depth to a positive number with something like:

this.swapDepths(10);
}

That should allow you to remove the movieclip that you attached to the stage in Flash at runtime using removeMovieClip.

Response to AS: Random Enemies 2006-09-10 04:08:17

Thanks so much. I could've swore that I went onto my flash and then copied the code in the bar but ohwell it worked.

Response to AS: Random Enemies 2006-09-12 01:58:44

Finally one that works! Thanks, you frickin' ROCK!

Response to AS: Random Enemies 2006-09-12 03:40:29

Great AS, I expect to be seeing this in the AS: Main

Response to AS: Random Enemies 2006-09-12 21:54:52

i tried making more than 1 type but the other type does not show up...only once in the begining.The first 1 only shows up.Can anyone tell me whats wrong?

Response to AS: Random Enemies 2006-10-27 16:25:59

Hopefully you can help me as well. I'm using the first method, and I put my enemy on a lower layer then the custom cursor I made. But when I test the game, the cursor only goes over the first enemy. And all the duplicate enemys are over the cursor. can this be fixed?

Response to AS: Random Enemies 2006-12-05 05:41:33

In the cursor:

this.swapDepths(99999);
}

Or something like that.

Response to AS: Random Enemies 2007-02-10 18:35:10

Hey, the codes are awsome! Thanks! I have one problem though; I gave my character a hit test to go to a "gameover" frame but, it only works with the first enemy that comes out. Any suggestions?

Response to AS: Random Enemies 2007-03-05 19:35:18

EXCUSE ME! Ummm... How do you make a HP bar say for an RPG fight stop when the meter reaches empty. Cuz mine doesn't stop when empty, the red bar filling the meter just goes passed and out of the meter. Basically it goes behind what was supposed to be the end of the Hp meter. HEELLPPP?!??!? PLZZ??!

No comment.. Oh wait that is a comment!

Whadda dumb siggy! Try sumthin better-----> Press Me!

Response to AS: Random Enemies 2007-08-18 19:14:14

I have a problem about this, i want both my enemy and bullet dissapear when they hit.
I use hittest and removeMovieClip but I cant, can you help me?

Response to AS: Random Enemies 2007-08-18 19:15:57

At 8/18/07 07:14 PM, Ismael92 wrote: I have a problem about this, i want both my enemy and bullet dissapear when they hit.
I use hittest and removeMovieClip but I cant, can you help me?

I forgot to say, my bullet's instance name is "bullet" (duh)

Response to AS: Random Enemies 2007-08-19 16:08:51

At 8/18/07 07:15 PM, Ismael92 wrote:
At 8/18/07 07:14 PM, Ismael92 wrote: I have a problem about this, i want both my enemy and bullet dissapear when they hit.
I use hittest and removeMovieClip but I cant, can you help me?
I forgot to say, my bullet's instance name is "bullet" (duh)