Make your enemies alive! This Flash ActionScript 2 game enemy AI script shows you how to add different enemy behavior to multiple enemies.
Each behavior is easily customizable; you can change movement speed, turn speed, aggression range & enemy graphic for each type of enemy.

thanks for making this AWESOME source code file.
I was wondering if you would consider making something like a pair of (2d / cartoon) eyes that follow your mouse, so not only do they have to rotate, which is the easy part, the pupils have to get closer to the center of the eye when you move your mouse closer. or at least over one of the eyes.
You could use this file and just use two of the triangles or something…
http://www.freeactionscript.com/2009/02/math-rotate-movieclips-toward-mouse/
well i mean i can make them rotate toward the mouse easy. the hard part is making the left eye center if i put the mouse right in the middle of the left eye and the same for the right. rather than it just like rotating wildly or in some off direction.
Werty2334; I’m glad you like it
imoncloudnine69; I’ve been thinking about what you asked. There is a way to make it, but how would something like that be used in a game?
As far as making it; I guess I would have a pupil (movieclip) follow the mouse if it’s distance (~50pixels) is <= center of eye.
Well i guess not used in the game, i have it used in my pause menu, that there are a pair of eyes i wanted to follow the mouse. but im sure there are things that you could use it for in a game.
Is there an easy way to avoid having to use the Math.sqrt() function? In most cases just using the square of the distances and comparing to the square of whatever else is much faster than having to run the costly square-root function…
Hi David, I’ve been working on a new ai script that does not use Math.sqrt. Here is a quick fla that shows the alternate code:
http://www.freeactionscript.com/download-test/inverseSquareRoot.fla
Great! It looks like your function estimates the square root with a level of precision determined by the number of iterations, correct? Out of curiosity, have you tried benchmarking this method against the Math.sqrt()?
Thanks!
Ok, listen, you are the creator of this amazing code correct? I really, really, really like your work, I saw a video of someone that manipulated this code weeks ago, since then, I’ve been trying to find the source code to use for my game (In AS3) I’m making. Please tell me, is there a version of this in AS3? I’d unfathomable appreciate it if you’re willing to remake this in AS3.
Also, here is the vid I saw: http://www.youtube.com/watch?v=GT-Reh3-4kQ
If you’d rather consider making something like this in As3, It’d be great!
Actually all of your work is awesome, so if there’s anything you made in As3 already, please let me know, but particularly this code catches my eye.
Anyhow, thanks for your awesome site and tuts. Looking forward to what the future holds.
David; I have not benchmarked it. I have been discussing different methods with a colleague (http://vipegames.blogspot.com/) and that was one that came up. Here is another method:
Normally you do something like this
dX = enemy.x - me.xdY = enemy.y - me.y
overlapDistance = enemy.radius + me.radius
if(Math.sqrt(dX * dX + dY * dY) < = overlapDistance)
{
Collision!
}
instead of square rooting the left side of the equation, you could also square the right side of the equation like this:
-
if(dX * dX + dY * dY <= overlapDistance*overlapDistace){
Collision!
}
-
The second method solves the equation but doesn't use a square root, which makes it a lot cheaper.
xoria; Thank you! I’ve seen the video, it’s great. I’ve been actually optimizing my ai code over the last month or so. I will release a new and improved version soon.
As far as AS3; I will start rewriting all my scripts from scratch in a couple of months.
Thank you!
pradvan; You are speaking of TOdorus from kirupa / flashKit I assume. He’s a really cool guy.
The method you just posted is exactly the one I was talking about in my first comment. I tried to implement it in your system and it is no problem for the aggro stuff, but for the turning behavior (which is the only part I am really concerned with to be honest) I could not figure out how to make it work properly.
Thanks for the replies!
hi.
i like your scripts they realy help me!
i have a question.
im having some troubles with the destroy enemy thing is used the destroy enemy from your weapon and enemy script but could you make an destroy enemy script for the enemy behvior script??
greetings
hi erikpro, here is a function that looks for a movieclip in an array, and removes it:
//to use://removeEnemy("myEnemyInstanceName");
//
function removeEnemy(searchTerm:MovieClip):Void {
for (var i = 0; i < enemiesArray.length; i++)
{
var tempItem:MovieClip = enemiesArray[i]
if (tempItem == searchTerm)
{
removeMovieClip(searchTerm);
enemiesArray.splice(i, 1)
}
}
}
thank you!
this has solved my problem =D
hi again everything works now as it should thanks to you!! =D
but i need 1 thing and i hope you can help me with that.
its like going to another frame but in that frame every “follower” must be destroyed and not respawning like a switch spawn or destroy all and not spawn.
Greetings
Erik
So you’re looking to destroy all the created enemies? Copy the removeEnemy function and modify it to remove everything instead of just one enemy.
ok thank you.
Greetings
Erik
Hi again i posted on the blood engine but the depth question i had was about this one.
you used here this script
var tempEnemy:MovieClip = _root.attachMovie(enemyLibraryClip, "enemy"+_root.getNextHighestDepth(),_root.getNextHighestDepth())but i dont want it to be at the top of everything and if i change the depth like you said its starts to act weird like not spawning etc
Hope you can help
Greetings
Reaven
Hey did you ever figure out a good way to simulate this kind of movement behavior without needing to use the Math.sqrt function?
Thanks!
erikpro,
try using different containers for different things. put all enemies in one container, effects in another, and arrange the depth the way you like.
what is happening right now is that you are trying to place 2 objects on one depth, resulting in things not working properly
david,
to be honest, i gave up on that. it does have speed benefits but it wasn’t worth spending more time on. I will give it another go in AS3
Hi.
i tried do do it in a diferent container but
then they wont follow anymore :S and the hittesting doesnt work anymore.
Hope you can help
Greetings
Reaven
Whatever is following whatever, needs to be in the same container.
Same with hit tests; Whatevet is doing a hittest on whatever, needs to be in the same container.
I really can’t offer any more advice as I don’t know how your game is structured or what you’re even trying to build for that matter. I showed you different tricks, now its up to you to figure out how/what to apply in your game.
Hey, this is exactly what I need for a game I’m thinking of. Have you had any progress with as3 version, I’d be happy to go through it with you if you’ve done anything with it or I’ll just make it and send you the code to put up here.
Talk soon,
Dave
Hi Dave,
I have not even began thinking about converting this to AS3. When I get around to it, I will definitely share it.
Thanks,
PR
thanks for awesome tutorial. Please remake this in AS3
Thanks
Here’s a question: What do I refer to enemies as? what would their instance name be? I want to add code to each enemy for typeB but I dont know what instance name to use.
Hi santhosh.paka,
Converting this to AS3 is very high on my to-do list. It should be ready for release some time this year.
PR
Hi GeorgeC,
There is no direct way to refer to enemies, unless all the enemy names correspond to the layer depth that you’re on (enemy1, enemy2, etc).
The best way to access the enemies is to loop thru the enemiesArray, since it holds all the enemies.
For example, if you want to do something to all enemies that follow:
for(var i = 0; i < enemiesArray.length; i++)
{
var tempEnemy:MovieClip = enemiesArray[i];
if (tempEnemy.mode == "follow")
{
// do something
}
}
Hope that helps.
PR
Thanks, I got it to work.
In AS3 I tried applying some detection code to my already established movement for enemies. They turn around when they hit objects and I though it was possible to trigger the turning around if the hero was close as well, but it does not seem to have any effect? How can that be?
// if hit a wall, turn around
if (enemies[i].hitWallRight || (enemies[i].x+30 > hero.x)){
enemies[i].moveLeft = true;
enemies[i].moveRight = false;
} else if (enemies[i].hitWallLeft || (enemies[i].x-30 < hero.x)) {
enemies[i].moveLeft = false;
enemies[i].moveRight = true;
}