Internet Commerce

Partners & Affiliates

Developer Channel


Featured Flash FLA
Gallery Downloads 11401 Flash Movies | 5 New Flash Movies Added
What's New | Top 100

Featured FLA

»  Author Miguel Panos
»  Title: Tarta
»  Description: It is a circle graph or pie chart that takes the data entered by user
»  More by Miguel Panos


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 4839 Flash Sites | 1 New Flash Links
What's New | Top 100 Flash Site

Featured Site

»  AuthorDeft Creative Ltd
»  Link: Home Page
»  Description: Portfolio site for .DeftCreative Ltd. A UK based web design studio specialising in flash websites, games and interactive CDROMs. With an emphasis on making things different.


Random Links | Add your own Flash Related Links
Flash Tutorials 1484 Tutorials 7 New Tutorials Added!
What's New | Top100

» Making Automatic Training Screen Capture Easily
» Create Undersea Life Animation
» Making Deinterlace Video with a low bitrate Easily
» How To Make A Simple Animation Using Christmas Clips
» Simple Step by step flash game tutorial Spot the diffrence
» How To Make A Moving Text Slide
» Create Flash Banner With Text Float Effect
» How To Make Zoo Photos Slideshow
» How To Make A Dolphin Photos Slideshow
» Unknown Tag: Title10
Random Tutorial | Add Site


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Actionscripting

Categories 2D Game Simple AI, moving and attacking
Author: Luke Knepper | Website: http://zazeran.com |

 
Page 1
1

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!)

1

» 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.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments