Basic A.I Tutorial: Enemy Attacks and Movement
In my past AI post, I taught you how to make an enemy attack the player when the player was in range. In this tutorial I will be expanding upon that post, teaching you how to make the enemy follow the player while the player is within range of the enemy, then attacking once it is within range to attack using actionscript 2 code. Here is what you will be making.
Creating the Player
- Open up flash and hit CTRL+F8 to bring up the create new symbol box.
- Make sure the Movie Clip option is selected, and name it whatever you want.
- Create your character as you want it to look.
- Drag the player onto the screen from your library and give it the instance name of player.
Creating the Health Bar
- Create a new symbol and make sure the Movie Clip option is selected.
- Now draw a rectangle that will represent your health bar.
- On frame 100 insert a new keyframe.
- On the frame 1, delete most of the colored inner part of the rectangle, until you only have a very small amount left on the left side.
- Now highlight frames 1 to 100 and bring up the properties panel (it’s on the bottom of the screen).
- Select Tween>Shape.
- Drag the health bar onto the stage and give it the instance name healthbar.
Creating the Enemy
- The enemy will be a bit different from creating the player and the health bar. You are going to need 3 movieclips.
- Create one movieclip that shows the enemy standing still.
- Create one movieclip that shows the enemy’s first type of attack.
- Create another movieclip that shows the enemy’s second type of attack.
- Have all of them? Now create a new movieclip once more.
- In this movieclip, have 3 frames.
- On the first frame, drag the enemy standing still movieclip onto the stage. Give this frame the name standing.
- On the second frame, drag the enemy with the first attack’s movieclip onto the stage. Give this frame the name attack1.
- On the 3rd frame drag the enemy’s second attack movieclip onto the stage. Give this frame the name attack2.
- Put the this code on all 3 frames:
stop();
- Now exit the enemy’s movieclip and drag the enemy movieclip with all 3 movieclips inside of it onto the stage.
- Give it the instance name of enemy.
Now it’s time to start adding code.
Put this code on the frame
var enemyTimer:Number=40;//creats a variable called enemyTimer with a value of 40 (2 seconds) var eScale:Number=100//creates a variable called eScale, which will be used for the enemy's _x scale. _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 }
Put this code on the player MovieClip
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 } }
Put this code on the enemy MovieClip
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
a=int(Math.random()*100)//creates var a which is an random integer between 1 and 100
if(a >= 0 && a < 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 (a >= 60 && a <= 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
}
}
if(distance < 160 && distance > 100){//if the distance between player and enemy is between 160 and 100… (to increase or descrease max and min range, just increase or decrease the numbers)
this._xscale = _root.eScale;
_x-=2;//make the enemy walk towards the player. Increase number to increase enemy’s speed.
}
if(distance > -160 && distance < -100){//if the distance between player and enemy is between -10 and -160… (to increase or descrease max and min range, just increase or decrease the numbers)
this._xscale = -_root.eScale;//turn the enemy in the proper direction
_x+=2;//make the enemy walk towards the player. Increase number to increase enemy’s speed.
}
}
I apologise for the lack of nice looking code frame, but it seems to mess up the & signs that are needed for the code. I decided I would rather you have the right code than make it look pretty. Please note that you will have to change the ‘ signs around attack1 and attack2 when the code says to gotoAndStop on them.
Now test your game. When you walk near the enemy it should start following you to a certain distance and when it comes within range will attack you.
That’s it for the movement and attacks AI tutorial. This should have your enemies smarter and more believable. Just don’t make them too good, gamers like having a chance to win.
Here’s the .fla for those of your who may need it.
Always remember to optimize your coding and file size so it will run efficiently and fast. You could also get better hosting (e.g. business hosting) so that your visitors will be able to view your site without having to wait for a long time.
Related posts:



May 14th, 2009 at 12:34 pm
thx very much!!!
August 5th, 2009 at 3:42 am
I can’t seem to download this.
August 5th, 2009 at 9:49 am
Thanks for the notice. I’ve updated it and it’s working now. Plus, the .fla is contained within a .zip file for quicker downloading.
September 30th, 2009 at 3:43 pm
thanx…very helpful
December 4th, 2009 at 2:47 am
how can i make an animation of the enemy running?
January 19th, 2010 at 3:11 pm
What do you mean that we will have to change the signs around for the “Gotoandstop” option in the enemies code?
February 15th, 2010 at 5:38 pm
how do I make it so that my enemies can’t walk over the hpBar? if u get ma picture…
my char, gets below my bars but my enemie walk above it. my character is not visible while he stands on the hpBar but my enemie is visible
February 15th, 2010 at 6:30 pm
My blog makes the quotation marks look different, so you may have to change them in flash Person.
I’m not quite sure what you mean. Could you elaborate a little.
February 16th, 2010 at 4:10 am
my enemies is coming from the top of my game. and my health bar is also at the top, so when my enemies are walking down the screen they walk ontop of the healthbar instead of under it.
thx for the quick answer :)
June 8th, 2010 at 7:33 pm
Woah nice tutorial. Very well explained! Thanks alot.
- Amy
June 9th, 2010 at 11:17 am
Thanks Amy, glad it could be of use to you :)
October 16th, 2010 at 4:42 pm
hey, nice tutorial, but im a beginner at AI coding, do you know any codes just for a stick man with a gun? like, a type of distance, the stick man would fire a bullet at me.
March 21st, 2011 at 9:56 pm
Nice tutorial. Im making a stickfigure platform shooting game and I was just wondering if you could help me out if I send you the FLA file. I would really appreciate it !
Its a really simple game but Im new to Flash and I have know idea how to finish this game (I started it)
btw im willing to pay you a reasonable amount
Please email me !
May 25th, 2011 at 4:31 pm
Hi, I used your tutorial and modified the script a bit to fit a game I was already making. It works really well! Except when it attacks, it just keeps attacking and doesn’t stop, and I don’t know what I can do to fix it.
May 28th, 2011 at 7:55 pm
Jaelin, it might have to do with the movieClip you made for the attacker. When the attack movieclip finishes, you should put in a code that tells the attacker to gotoAndStop on a frame that has the attacker standing still.
August 20th, 2012 at 7:34 am
this is really interesting, can flash game ai have the same intelligence as actual pc game ai?
August 20th, 2012 at 1:33 pm
It really depends on how you do the programming gamenoodle. In general, pc games will run faster code than Flash, which allows for better AI. But that said, if you are a great programmer in Flash you could probably do more advanced AI code than a mediocre developer making games for the pc.
January 17th, 2013 at 10:28 pm
Hi I’m making a top down shooter game so i would like to know how to make the enemy follow the charracter even when hes moving up and down because he follows when he moves left and right but not up and down.
Thanks.
February 18th, 2013 at 2:13 am
Thank’s alot it’s very helpfull ;)))