» Home » Movies » Tutorials » Submissions » Sound FX » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops
Search tutorials
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 Votes: 8 1 2 3 4 5 6 7 8 9 10 (10 being the highest) » 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) » Forums More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.