Be a Supporter!

AS: Random Enemies

  • 5,826 Views
  • 33 Replies
New Topic Respond to this Topic
Kabomb
Kabomb
  • Member since: Sep. 25, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
AS: Random Enemies 2006-04-22 02:04:16 Reply

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:

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.

fuzz
fuzz
  • Member since: Jun. 2, 2004
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to AS: Random Enemies 2006-04-22 04:20:53 Reply

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

Depredation
Depredation
  • Member since: Sep. 5, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to AS: Random Enemies 2006-04-22 05:04:15 Reply

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


BBS Signature
Fuckle
Fuckle
  • Member since: Sep. 14, 2005
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to AS: Random Enemies 2006-04-22 05:32:34 Reply

Nice.Looks like alot of code


Get Fuckle'd

BBS Signature
shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Random Enemies 2006-04-22 05:55:56 Reply

cool tutorial rather basic but explained well..

yam
yam
  • Member since: Mar. 12, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Random Enemies 2006-04-22 06:01:37 Reply

Great Tutorial!

Im checking if it works...

WolfAkela
WolfAkela
  • Member since: Dec. 19, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Random Enemies 2006-04-22 06:03:07 Reply

This'll be useful! Kudos! :D

Canadian-Jeff
Canadian-Jeff
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to AS: Random Enemies 2006-04-22 18:26:32 Reply

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

Canadian-Jeff
Canadian-Jeff
  • Member since: Sep. 17, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to AS: Random Enemies 2006-04-22 19:37:07 Reply

^^^^^ 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.

Trunks
Trunks
  • Member since: Jul. 31, 2005
  • Offline.
Forum Stats
Member
Level 22
Musician
Response to AS: Random Enemies 2006-04-22 19:55:10 Reply

Great tut, I loved how you explained every step.

Take that, Copy&Pasters!


Web developer, software engineer, musician, all-around nerd.

Reiewer
Reiewer
  • Member since: Aug. 23, 2005
  • Offline.
Forum Stats
Member
Level 19
Gamer
Response to AS: Random Enemies 2006-04-22 20:00:55 Reply

niiice

Spam-Bomb
Spam-Bomb
  • Member since: Jul. 29, 2006
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to AS: Random Enemies 2006-08-02 00:32:23 Reply

sucks ass. code will not work

Dog
Dog
  • Member since: Feb. 8, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Random Enemies 2006-08-02 03:12:07 Reply

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

PyroGreg
PyroGreg
  • Member since: Jun. 22, 2006
  • Offline.
Forum Stats
Member
Level 24
Programmer
Response to AS: Random Enemies 2006-09-10 03:27:19 Reply

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();
}
}

Please help me

BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: Random Enemies 2006-09-10 03:34:03 Reply

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();
}
}

Please help me

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.

PyroGreg
PyroGreg
  • Member since: Jun. 22, 2006
  • Offline.
Forum Stats
Member
Level 24
Programmer
Response to AS: Random Enemies 2006-09-10 03:37:49 Reply

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.

PyroGreg
PyroGreg
  • Member since: Jun. 22, 2006
  • Offline.
Forum Stats
Member
Level 24
Programmer
Response to AS: Random Enemies 2006-09-10 03:42:08 Reply

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

http://s88.photobuck..=1&addtype=local

BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: Random Enemies 2006-09-10 04:04:54 Reply

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

http://s88.photobuck..=1&addtype=local

As much as I would like to log into your account so I could see your example I can't because I don't have your password.

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:

onClipEvent (load) {
this.swapDepths(10);
}

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

PyroGreg
PyroGreg
  • Member since: Jun. 22, 2006
  • Offline.
Forum Stats
Member
Level 24
Programmer
Response to AS: Random Enemies 2006-09-10 04:08:17 Reply

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.

MKRH
MKRH
  • Member since: Jul. 4, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Random Enemies 2006-09-12 01:58:44 Reply

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

HappilyNeverAfter
HappilyNeverAfter
  • Member since: Jul. 23, 2006
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Random Enemies 2006-09-12 03:40:29 Reply

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

Stives1111
Stives1111
  • Member since: Sep. 9, 2006
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to AS: Random Enemies 2006-09-12 21:54:52 Reply

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?

Zeek664
Zeek664
  • Member since: Jun. 14, 2006
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Random Enemies 2006-10-27 16:25:59 Reply

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?

chinkeeyong
chinkeeyong
  • Member since: Aug. 23, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS: Random Enemies 2006-12-05 05:41:33 Reply

In the cursor:

onClipEvent (load) {
this.swapDepths(99999);
}

Or something like that.

J-Diezel
J-Diezel
  • Member since: Jul. 11, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: Random Enemies 2007-02-10 18:35:10 Reply

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?

Android11
Android11
  • Member since: Jun. 2, 2006
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Random Enemies 2007-03-05 19:35:18 Reply

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!

BBS Signature
Ismael92
Ismael92
  • Member since: Jul. 27, 2007
  • Offline.
Forum Stats
Member
Level 31
Blank Slate
Response to AS: Random Enemies 2007-08-18 19:14:14 Reply

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?

Ismael92
Ismael92
  • Member since: Jul. 27, 2007
  • Offline.
Forum Stats
Member
Level 31
Blank Slate
Response to AS: Random Enemies 2007-08-18 19:15:57 Reply

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)

Ismael92
Ismael92
  • Member since: Jul. 27, 2007
  • Offline.
Forum Stats
Member
Level 31
Blank Slate
Response to AS: Random Enemies 2007-08-19 16:08:51 Reply

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)

Please help me, im using this tutorial for bullets. Its important!

Kodman15323
Kodman15323
  • Member since: May. 6, 2007
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Random Enemies 2008-02-13 00:53:21 Reply

For some reason half of mine seem to "spawn" off screen.