Basic A.I: Attacks
What you will learn: how to make an enemy attack your character with a different attack, based on a percent, every 2 seconds. The attack will make your health go down.
Creating the MovieClips:
1. Make a character MovieClip.
2.Make a healthbar.
3. Make the enemy:
- create three frames within the enemy with a movieclip inside each frame
- put the stop; action on each frame
- Make sure the end of each MC within the frame has this actionscript on their last frame: _root.enemy.gotoAndStop(’standing’);//since we do not want the attack to loop, we send it to the standing frame.
- one frame is the enemy standing still (label standing)
- another is the enemy’s first attack (label the frame attack1)
- the last is the enemy’s second attack (label the frame attack2)
1. First you’ll need to be able to move your character MC. To do this use a basic movement code:
onClipEvent(enterFrame){//every frame if(Key.isDown(Key.LEFT)){//if left key is down this._x-=5;//this moves 5 pixels to the left } if(Key.isDown(Key.RIGHT)){//if the right key is down this._x+=5;//this moves 5 pixels to the right } }
2. Give your character the instance name player.
3. Now you will need a health bar. Use the tutorial here.
4. Now, give your health bar the instance name healthbar.
5. Next, put this code onto the frame:
enemyTimer=40;//creats a variable called enemyTimer with a value of 40 (2 seconds) _root.healthbar.gotoAndStop(100);//healthbar stops at frame 100 onEnterFrame= function(){//every frame if (_root.enemyTimer==0){//if the timer equals 0 _root.enemyTimer=40;//the timer resets to 40 } else _root.enemyTimer-=1;//otherwise, the time goes down by 1 every frame }
6. Finally, you can add the A.I code to your enemy. Put in his MC:
onClipEvent(enterFrame){//event that happens every frame
distance= _root.enemy._x- _root.player._x;//declares a variable called distance that tells the distance between the player and enemy
if(distance<100 && distance >-100 && _root.enemyTimer==0){//if the distance between the player and enemy is less than 100, greater than -100 and the timer equals 0
rNumber=int(Math.random()*100)//creates var rNumber which is an random integer between 1 and 100
if(rNumber >=0 && rNumber <60){//if a is greater than 0 and less than 60
_root.enemy.gotoAndStop(‘attack1′);//enemy goes to attack frame 1
_root.healthbar.gotoAndStop(_root.healthbar._currentframe -= 5);//healthbar goes down by 5
} else if (rNumber>=60 && rNumber<=100){//if a is between 60 and 100
_root.enemy.gotoAndStop(‘attack2′);//enemy goes to attack frame 2
_root.healthbar.gotoAndStop(_root.healthbar._currentframe -= 8);//health goes down by 8
}
}
}
7. Test your file. You should be able to move your character over to the enemy and have the enemy attack you, making your healthbar go down.
8. Here is what the final product should look like.
Related posts:





May 5th, 2008 at 3:09 am
makes it sound so simple!!
March 4th, 2009 at 11:18 am
thank you so much, good sir.
this has helped me immensely :D
March 4th, 2009 at 6:34 pm
No problem. Glad I could help.
March 26th, 2009 at 9:05 pm
hi, could you put in some action script so the ememy will follow the player?
thanks [=
March 29th, 2009 at 2:07 pm
tat was ok but even if the character moves out of the way of the attack he still gets hurt
March 29th, 2009 at 2:49 pm
It all has to do with the radius you set. The attacks need to be the length of the radius, plus some to compensate for character movement.
April 29th, 2009 at 6:46 am
Could you help me out it all works but i want the enemy to have a walking animation could u send me s edited version to this script with that or a url to a existing tutorial.
May 7th, 2009 at 3:00 pm
I hav a problem. Ive used this script for 1 enemy that i dragged from the libary into the mainscene. My characters attacks can kill the enemy “visualy” with unloadMovie, but somehow the enemy script itself that lowers my characters healthbar keeps continuing.(like fighting an invisible ghost). how can i solve this issue?
May 7th, 2009 at 4:07 pm
It may be because the enemy was killed when you were within the distance of the enemy. Thus, the distance will remain at the number it was when the enemy died.
If you are using a different variable for each enemy’s distance, you could probably set the distance variable of that specific enemy to null when the enemy dies.
May 8th, 2009 at 7:49 am
I’ve followed your pointers and managed to solve the problem. Everything works perfect now. thanks alot.
Hmm perhaps another small issue, if i may be so bold to ask:
How can i add a walking part to the cycle of the enemy?
What needs to be done to make it work(i think)
I need to add some code to the enemy actions that tells it to switch from “standing” frame to “walking” frame as soon as the player gets within its range and then stops “walking” if the player is within its attack range, thus starting the attack-standing-attack-standing cycle, switching back to walking if the player moves away from enemy.
But i hav no idea how to implement that into the current code.Idunno if solution is simple, but i tend to think difficult. lol.
soz, for bad grammar if there is any. i’m Dutch.
May 8th, 2009 at 9:35 am
Take a look at my other tutorial on attacks and movement.
You’ll want to be looking at the part where it says:
if(distance < 160 && distance > 100){….
In that code, you can add this.gotoAndStop(‘walking’); where walking is your walking frame.
Also, you’ll need to add a gotoAndStop(’standing’); code for when the distance is greater than 160 or less than -160.
Hope that wasn’t too confusing. If you need any help, just ask and I’ll try to explain it better.
May 8th, 2009 at 11:52 am
Well, i’ve managed to integrate the “walking part”. The enemy now succesfully switches to “walking” when the player gets near. attack-standing-attack works aswell. only problem that remains is that the enemy stays in “walking” even when the player is out of its aggro range.
You’ve guessed it, I didn’t get that last part lol.
But i did try something out.
//This “walking” part below works yey//
}
if(distance 100){
this.gotoAndStop(‘walking’);
this._xscale = _root.eScale;
_x-=2;//make the enemy walk towards the player.
//tried to let the “standing” work by adding the code below//
} else {
if(distance > 160 && distance < 100){
this.gotoAndStop(’standing’);
}
}
But its not working lolz. =3
May 8th, 2009 at 4:56 pm
I think the distance code you are using will only work when it is between those two numbers. You want it to be:
if(distance > 160 || distance < -160){
this.gotoAndStop(’standing’);
}
I’m assuming you’re using 160 as a distance for the walking to begin. Try that and tell me if it works any better.
May 9th, 2009 at 6:35 am
Yup, it works now =D.
Thanks alot for the support. I know some actionscripting, but as i’ve mentioned b4, i tend to approach a problem with to many things in mind, especialy when it involves maths lol.
Maybe other questions will arise on how to improve the enemy later on, but for now i can continue with my small project. Keep up the good work Sir.
May 9th, 2009 at 9:12 am
Good to hear that. Glad I could help.
May 10th, 2009 at 9:08 am
Lol, i’m back again(uhoh). I’ve noticed in the tutorial you used a (logical) 1 vs 1 situation. Currently i’m trying to use the enemyclip multiple times in the same scene.(i made a game level and manually want to place them on various locations on that map)I tried making additional layer for each enemy and giving them a diffrent instance name like enemy2, etc, which works to some the degree.Only backdrop is i cant make any nesecary changes inside a copy of the original movieclip
(_root.enemy.gotoAndStop(’standing’) cant be altered to (_root.enemy2.gotoAndStop(’standing’) for instance. Cuz all copies are based on the original movieclip so it will affect them all. So my question: Is there a simple way to solve this? I know there’s this method that involves creating a function for that enemy, but i was kinda hoping not having to figure that out. =P Thanks for your help in advanced.
May 10th, 2009 at 6:14 pm
Sorry, but I’m a bit confused. Where is the code residing currently? Are the movieClips being duplicated by actionscript or are you duplicating them on the stage?
May 11th, 2009 at 3:06 am
I’m duplicating them on stage
May 11th, 2009 at 5:23 am
But each duplicate of the enemy mc(with the req actions) is placed on a diffrent layers so they wont interfere with each other. The problem im kinda facing is the actions inside the original mc itself…cuz i need to change all rooting from enemy1 to 2, 3, 4, etc. So in general that would mean i need to create new mc’s that uses the same enemy sprites but with diffrent scripting. Its possible, but that would make it all very messy.(20 enemys = 20 layers, each with their own actionscript lol). Now its getting complicated =S
May 11th, 2009 at 2:30 pm
This code is going to get pretty complex if you have that many MC’s. You can’t use the same distance variable for all the enemies, as I’m assuming they aren’t all standing right next to each other. This means you’ll need 20 different variables for the distance between the player and each individual enemy.
If you’re talking about the _root.enemy within the enemy’s code, you may be able to change that to ‘this’. I would test it with each change you make though, to make sure it still works.
Sorry if that doesn’t help much. I’m getting a bit confused myself now on how to get it to work.
May 11th, 2009 at 3:52 pm
Nah, its ok =P I’m just very Enthusiastic on building my 1st game.I’ll try the “This” thing. And the distance variable doesnt seem to interfere with each other. It’s a platform game, But i’m trying to wrap it up in a unique style and scenario. I can show you a sample once i’ve finished building it If you want. Actually, i need to build it within 6 weeks from now o_o. It’s for schoolproject you see.
May 11th, 2009 at 5:20 pm
Hm, hm lol.. It’s seems the “This” thing is working thanks., but i keep getting this weird error debug screen:
“Expected a fieldname after ‘.’ Operator”.
But this is… harmless yes? =P
May 11th, 2009 at 5:26 pm
Try it without the ‘.’
Instead of:
this.
It might be:
this
May 12th, 2009 at 7:49 am
The solution was THAT simple and i failed to see that lol.
Kinda Noobish hehe =P
October 2nd, 2009 at 2:45 am
bro wat is the meaning of MC im just starting using Flash
and i see ur Tutorial and i know it will help me alot if i understand thx^_^
October 3rd, 2009 at 7:52 am
MC means Movie Clip. It’s a type of symbol.