Top Veiw AI
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:
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:
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 = 3*Math.cos((this._rotation*Math.PI)/180);
var ys:Number = 3*Math.sin((this._rotation*Math.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.
Now if you test your movie (Ctrl+Enter) you should get something like this: [Click Here].















Woot helped so much.Once again thatnks a lot this helped soo much.
As i understand this was the basic tut for the snake game?
Well its awesome.
Hey this is like something i requested except i asked for it to follow the cusor not a item.
@Daniel: You could use this for a snake game but for mine I used kinematics.
@Kurt: That can easily be changed. Where it says enemy._x and enemy._y you could replace with _xmouse and _ymouse.
haha this whould save me some time if you did post it like 2 weeks erlyer when i figerd it out be myself
sory if you find my spelling incorect becuse i sucks on spelling =)
LOL, on mine, it started to make the enemy follow the cursor.
I changed the script to this:
function convert(radians:Number):Number {
2.
degrees = radians*(180/Math.PI);
3.
return degrees;
4.
}
5.
player.onEnterFrame = function(){
6.
startDrag(this,true);
7.
}
man.onEnterFrame = function() {
2.
var adj:Number = enemy._x-this._x;
3.
var opp:Number = enemy._y-this._y;
4.
var rot:Number = Math.atan2(opp, adj);
5.
this._rotation = convert(rot);
6.
var xs:Number = 3*Math.cos((this._rotation*Math.PI)/180);
7.
var ys:Number = 3*Math.sin((this._rotation*Math.PI)/180);
8.
this._x += xs;
9.
this._y += ys;
10.
};
Mouse.hide()
I also put this on the ENEMY movie-clip:
onClipEvent (enterFrame) {
difx = _root._xmouse - _x;
dify = _root._ymouse - _y;
difx2 = difx / 20;
dify2 = dify / 20;
setProperty(”", _x, _x + difx2);
setProperty(”", _y, _y + dify2);
}
Is you wanna change what speed the enemy goes in that last piece of AS, change the 20 in ‘dify2 = dify / 20;’ and ‘dify2 = difx / 20;’ to:
A lower number:
Faster enemy
A higher number:
Slower enemy
First Frame code not working:
Scene=Scene 1, Layer=Layer 1, Frame=1: Line 1: ‘{’ expected
function convert(radians:Number):Number {
Scene=Scene 1, Layer=Layer 1, Frame=1: Line 4: Unexpected ‘}’ encountered
}
Also, could you make the man move with the arrow keys, not the mouse?
Yea, just replace the startDrag function with the script from the ‘Moving a movieclip with the arrow keys’ tutorial.
Also that error is really weird. It is not noticing the { on the first line, and then it is thinking the } that should be closing the { isnt supposed to be there.
(I love this “reply” link!)
Phew, I might need to use the mouse for something else. Also, I’m using Flash 5, so that may be causing the error, but I’m still confused as most of your Flash 8 tuts work for me….
And thanks for always helping me with my games.
I think they changed the way to declare a function from AS 1.0 to AS 2.0, since AS 2.0 was made for OOP (object oriented programming… i think).
probably. Hey. Before i was banned from deviantart, I showed you my MSN Beta, and you told me to make it so you could type. Well I have, from scratch (my internet was down) and here’s the link: CLICK HERE (it’s girly, but please try to ignore that, i’m going to change it…)
That is really cool. I like how to message also sends when you push enter.
Oh, thankyou! I just used the release & keypress function. You do know how old I am right? And why I was banned from dA?
Wait… I just remembered that I told you on MSN.
Yes. You are going to be very good at flash by the time you are my age. I cant believe that you can even use flash at your age
There are 2 computer freaks that EVERYONE looks up to at my intermediate, I’m one of them. And there is like, 560 students there. 558 are not computer freaks.
Heh… I dont really know anyone, who isnt over the internet, that does the stuff I do.
thats pretty kool, at first i was like, that’s it it seems pretty easy to program but then i couldn’t figure it out in my head so i was like >:(
not very good with text
i mean i’m not u are
OMG how did u make that i really wanna make one
can someone help me ive just started and i dont get it atall.
Ive made the enemy and man circle and put in the action script but it doesnt work.help
Sorry if some things are spelt wrong not very good at it
The man and the enemy circles need to be Movie clips. Put the instance names on them that it tells you in the tutorial, then put the code onto the frame.
Does anyone have a working .fla?
i used the scripts above in every possable combonation and on every MC.
and i cant figure out whats going wrong..
basicly i have this…
on frame.
function convert (radians:Number) :Number {
degrees = radians+(180/Math.PI);
return degrees;
}
man.onEnterFrame = function(){
startDrag(this,true);
}
enemy.onEnterFrame = function() {
var adj:Number = man._x-this._x;
var opp:Number = man._ythis._y;
var rot:Number=Math.atan2 (opp, adj);
this._rotation = convert(rot);
var xs:Number = 3+Math.cos((this._rotation+Math.PI)/180);
var ys:Number = 3+Math.sin((this._rotation+Math.PI)/180);
this._x += xs;
this._y += ys;
};
and nothing in the MC’s. Does anyone have any clues?
Have you given the MCs instance names? All lowercase?
Which version of flash are you using and is flash giving you any errors?
On mine the cursor one is really fast while the other one is too slow how do I fix this?
Well the cursor on is going to go as fast as your cursor, so move your mouse slower.
Also in the tutorial it tells you which part makes the MC move, just change the numbers so it goes faster.
I was about to post that, but I wasn’t quite sure and canceled….
check out what i made with this tut:
http://www.swfup.com/file/10230
tell me what you think!
Nice work
How do you make it so you can shoot bullets the way you are facing?
I will try and do a tutorial for that. It is a cross between the normal bullet tutorial and this tutorial.
Sorry I posted this on the wrong on I wanted the one were it points towards the mouse
You would use the same script that makes the enemy face the mouse in this tutorial.
Comment: excellent! it is very sleek
Q.however one difficulty, i switched the AI to the enemy following the man. Which works fine. (I also made it so that I move my man with the arrow keys) My problem is when I make another enemy (copy and paste the AI one) it doesn’t move.
You would need to make new variable names etc…
observe >:D http://www.swfup.com/uploads/swf-11892.swf
Okay… ummm how do I say
enemy and enemy2.onEnterFrame = function() { ?
nvm i found out how to do it
how do i make it so an object can only move in a certain space
i have a startdrag function on a circle but i only want it to be able to move inside a square i made.
plz help
if(!circle.hitTest(square)){
stopDrag();
}else{
startDrag(etc…);
}
hey i really love these tutorials and they are helping me alot with my flash games design class!
i was just wondering tho, if u had done or know how to make it to the areal view AI moves around by itself minding its own business kinda thing unless u get into its threat range? thanks i really really need some help with that
Try searching for the Pythagorean theorem, which can help you find if something is in a circular range of something else.
If you wanted the enemy to move left to right you could have something like:
enemySpd = 5;
onEnterFrame = function(){
enemy._x += enemySpd;
if(enemy._x < NUM || enemy._x > NUM){
enemySpd *= -1;
}
}
Put that on the frame and replace what needs to be replaced.
Awesty can you make a tutorial where i can learn to make a top-down shooter,or can you give me a code to where my enemy. follows my man but my man moves with the arrow-keys and when my enemy collides with my man my healthbar goes down…or you could just make a top-down shooter tutorial.
Thxnz
Or how about I just make the whole game for you?
Honestly you will never learn anything if I just give you the code. Open flash and have a try. If you don’t understand something hit F1 and search for what you need to know.
That’s how most people do it.
Sorry to throw this in here randomly, but I was wondering if there was a free site I could upload my completed flash games to, like a photobucket for flash type thing.
I was also wondering if someone could teach me how to embed them on a site.
Thanks muchly.
You could submit it to Newgrounds or deviantArt. Your flash needs to be “approved” on newgrounds though.
Or you could just upload it to Image Shack if you don’t want to have to register.
Here is a link that explains how to embed flash:
http://www.w3schools.com/flash/flash_inhtml.asp
How would I make it so when the enemy hits me, code is executed? I have tried various hitTests, I don’t know what to do. Thanks.
Hey, nice tutorial, it’s great..!
I’m having the same problem Bryce K was having
“Okay… ummm how do I say
enemy and enemy2.onEnterFrame = function() { ?
”
…
“nvm i found out how to do it”
I have a turret like object that fires bullets via attachMovie (shot1, shot2 shot3 etc) And I’m trying to create a missile-like object that will chase down the cursor.. I’m not sure if the movement from this tutorial will give the best effect but I just can’t understand how to get the rotation right..
Long story short I need to know what ‘Bryce K’ knows >:O
Any ideas?
Thanks for considering
~Harley
i dont get it, every tutorial i do here it is some how always correct and is always telling me somthing like this **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Statement must appear within on/onClipEvent handler
function convert(radians:Number):Number {
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Statement must appear within on/onClipEvent handler
enemy.onEnterFrame = function(){
Total ActionScript Errors: 2 Reported Errors: 2
grr… can you help me??