Enemy Behavior - Run Away & Follow Player
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.
27 Comments to Enemy Behavior - Run Away & Follow Player
thanks for making this AWESOME source code file.
April 10, 2009
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.
April 10, 2009
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/
April 11, 2009
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.
April 14, 2009
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.
July 5, 2009
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
July 6, 2009
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!
July 16, 2009
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.x
dY = 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!
July 16, 2009
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!
August 5, 2009
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)
}
}
}
August 6, 2009
thank you!
this has solved my problem =D
August 6, 2009
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.
August 6, 2009
ok thank you.
Greetings
Erik
August 27, 2009
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
August 27, 2009
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
August 28, 2009
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.
September 29, 2009
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
Leave a comment
You must be logged in to post a comment.
CategoriesLinks |

April 9, 2009