Top View AI - Page 1
Posted by : awesty on Apr 21, 2008
In this tutorial you will learn how to make a top view AI (artificial intelligence) system. You will need Flash 8 or Flash MX 2004 and know some actionscript and possibly some trigonometry.
Click here to see an example of what we will be making.
Start off by making two circles on the stage. Make that both MCs (MovieClips) and give one and instance name of 'man' and one an instance name of 'enemy' (without the quotation marks).
Now enter this code on the first frame: function convert(radians:Number):Number { degrees = radians*(180/Math.PI); return degrees; } enemy.onEnterFrame = function(){ startDrag(this,true); }
That first part is just a function that converts radians to degrees. The rest means means 'When this frame is entered (so that would be 36 times a second for me, since my fps is 36), make the mouse start dragging this MC. Which is the enemy MC'.
Enter this code underneath the code I just gave you:
man.onEnterFrame = function() {
var adj:Number = enemy._x-this._x;
var opp:Number = enemy._y-this._y;
var rot:Number = Math.atan2(opp, adj);
this._rotation = convert(rot);
var xs:Number = 3Math.cos((this._rotationMath.PI)/180);
var ys:Number = 3Math.sin((this._rotationMath.PI)/180);
this._x += xs;
this._y += ys;
};
Line 1: When this frame is entered Line 2 - 3: Declaring to variables. One is the _x distance of the two MCs and the other is the _y distance. Line 4: **This declares the variable containing the angle (in radians) between the two MCs. It uses the trig function tan to get the angle. **Line 5: **Makes the man MCs rotation equal to the rot variable. It also converts the rot variable to degrees using the function you were given. **Line 6-9: Lines 6 and 7 use a bit of script that makes the MC move in the direction it is rotated too. The number at the front can be changed to how fast you want it to move. Lines 8 and 9 make the MC move in that direction.

no comment
Add comment