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

  1. Open up flash and hit CTRL+F8 to bring up the create new symbol box.
  2. Make sure the Movie Clip option is selected, and name it whatever you want.
  3. Create your character as you want it to look.
  4. Drag the player onto the screen from your library and give it the instance name of player.

Creating the Health Bar

  1. Create a new symbol and make sure the Movie Clip option is selected.
  2. Now draw a rectangle that will represent your health bar.
  3. On frame 100 insert a new keyframe.
  4. 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.
  5. Now highlight frames 1 to 100 and bring up the properties panel (it’s on the bottom of the screen).
  6. Select Tween>Shape.
  7. Drag the health bar onto the stage and give it the instance name healthbar.

Creating the Enemy

  1. The enemy will be a bit different from creating the player and the health bar. You are going to need 3 movieclips.
  2. Create one movieclip that shows the enemy standing still.
  3. Create one movieclip that shows the enemy’s first type of attack.
  4. Create another movieclip that shows the enemy’s second type of attack.
  5. Have all of them? Now create a new movieclip once more.
  6. In this movieclip, have 3 frames.
  7. On the first frame, drag the enemy standing still movieclip onto the stage. Give this frame the name standing.
  8. On the second frame, drag the enemy with the first attack’s movieclip onto the stage. Give this frame the name attack1.
  9. On the 3rd frame drag the enemy’s second attack movieclip onto the stage. Give this frame the name attack2.
  10. Put the this code on all 3 frames:
    stop();
  11. Now exit the enemy’s movieclip and drag the enemy movieclip with all 3 movieclips inside of it onto the stage.
  12. 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:

  1. Basic A.I: Attacks