Tutorials Home
What's New
Top Rated
Submit
myTutes
Random!
2D Game Simple AI, moving and attacking
Author: Luke Knepper
| Website: http://zazeran.com |
This is a little bit of Action Script that will give you a basis on which to
make a simple AI. I basically guessed and checked until I figured out the
correct algorithms. This is based on a set of axes whose center is the symbol
this is in. I changed the axes by turning them 90 degrees so you have a new set
so that you can decide which quadrant the other symbol is in, and the middle of
these quadrants is a direction for this symbol to move to get to it. Made for a
2D game where the symbols go up, down, left, and right.
function locate() { //function name is locate()
xd=this._x-_root.guy1._x //the difference in-between this
symbol's x and the other symbol's
yd=this._y-_root.guy1._y //the difference in-between this
symbol's y and the other symbol's
npx=(xd+yd)/Math.pow(2,1/2) //A little bit of math to change the
corrdinate
npy=(yd-xd)/Math.pow(2,1/2) // "
"
if(npx>=0 && npy>=0) { //Assumes first quadrant identities
this._rotation=0 //set's the symbol facing up (North)
this._y-=2.5 //moves the symbol up
return 1 //returns a value of 1 for first quadrant to be used in
other functions if needed
} else if(npx>=0 && npy<=0) { //Assumes second quadrant identities
this._rotation=270 //Sets the symbol facing left
this._x-=2.5 //moves the symbol to the left
return 2 //return 2 for second quadrant for use in other funcitons
} else if(npx<=0 && npy>=0) { //assumes 4th quadrant identities
this._rotation=90 //sets the symbol facing right
this._x+=2.5 //moves the symbol right
return 4 //returns 4 for 4th quadrant/right
} else { //only quadrant left is down (3rd)
this._y+=2.5 //moves down
this._rotation=180 //sets facing down
return 3 //returns 3 for 3rd quadrant/down
}
}
What this does is set the symbol moving towards the average direction that
the other symbol is in. It makes a killer homing device that is very hard to
defeat if you have the sprite attack. Speaking of which...
if( (this._x <_root.guy1._x +100 && this._x> _root.guy1._x-100) && (this._y
<_root.guy1._y + 100 && this._y> _root.guy1._y -100)) { //an if statement that
I figured out. If the other symbol is within 100 px of this symbol it will do
the action.
ran=Math.ceil(Math.random()*20)-1 //I added this to only make the symbol
attack some of the time because it was too hard to beat if it was constantly
attacking. can be removed if you wish. the whole math statement can be replaced
with random(20) if you wish, I just couldn't remember the code at the time....
if(ran==0) punch() //remove the if statement if you removed the random above.
the punch() function is my attack function, you will want to make your own
attack functions and put them here
}
Of course, the simplest way to make an AI is to have a lot of different
functions that do different things (move, attack...) and then activate them when
a condition returns true. I suggest you play around with these to get them how
you want, and in the meantime try inventing some of your own. Feel free to
modify, use and share the action script in this tutorial, however please don't
redistribute this tutorial, just give a link to it please.
Thanks! Luke Knepper (By the way, please visit zazeran.com and give me
feedback!)
| » Level Intermediate |
|
|
Added: : 2006-12-05
Rating: 9.13 Votes: 8
Hits: 1645
|
| » Author |
|
Luke has experience with Java, Flash/ActionScript, and PHP. Codes and writes programs a lot, enjoys making games. Has a small game website, zazeran.com .
|
| » Download |
|
Download the files used in this tutorial.
|
|
Download (0 kb)
|
|
Get conversion and unzipping tools
for PC and Mac here!
|
| » Forums |
|
More help? Search our boards for quick answers!
|
|
Please rate this tutorial, 10 is the top rating, you can also click the
comments link to read/write a review.
|
|
|