Basic Artificial Intelligence Part 2
In this tutorial we will continue from where we left of in my ‘Basic Artificial Intelligence Tutorial’. I recommend you read that tutorial before you continue with this one. In this tutorial we will learn how to make a camera set of an alarm when your character is in its field of vision and how to make something follow your character.
If you had taken the other tutorial, you should have already made the camera, your character and the guy that follows it, but here is what to do: (If you have done that tutorial you can skip this)
First of all, you need to make the character you control, his friend that will follow him, the enemy that chases and attacks/flashes when it hits him and a camera type thing.
The and his friend are simple, each are just one movieclip with one frame (You can do it with a walk cycle etc.). The camera type thing is one movieclip with 2 frames inside it. On the first frame, you need a movieclip of the camera rotating. Make sure you add the field of vision, even if it is transparent. You need it or it won’t work. On the second frame add an MC of the camera setting of an alarm of something
For the enemy, you need two frames as well (a MC with two frames inside it). On the first have you enemy as normal, and on the second have it attacking or something, just as long as it looks different.On all of those four frames (the 2 from the enemy and the 2 from the camera) add this:
stop();
All does is stop the movieclip from looping.
For the actions for your character and enemy you will have to go and read the tutorial yourself.
Put this code on your characters follower:
if(this._x < (_root.man._x - 25)){
this._x += 3;
}
if(this._x > (_root.man._x + 25)){
this._x -= 3;
}
}
That code is pretty simple. The first part, onClipEvent(enterFrame){ means when this frame is entered, run the following script.
if(this._x < (_root.man._x - 25)){
This means, if this MC’s _x coordinates and less than ‘man’s _x coordinates minus 25, do the following. So if the following MC’s x coordinates are 10, and ‘man’ _x coordinates are 60, 10 < (60-25), or 10 < 35, since its true it will do the following code.
this._x -= 3;
All this means is this MC’s _x coordinates decrease by 3, in other words move left.
The rest of the code is exactly the same but so it moves right.
Now place this code on your camera:
if(this.hitTest(_root.man._x, _root.man._y, true)){
this.gotoAndStop(2);
}else{
this.gotoAndStop(1);
}
}
The only part of that code you should not understand is the hitTest. This hitTest is different form the ones I usually use in the tutorials. It works like this:
MovieClip.hitTest(x, y, ShapeFlag);
The first part is the same for the other type of hitTest, so you should already know that. The next part (the parameters) is the different part. For the first one, x, you put the MC’s _x position, or the position you want to work when the MC’s _x coordinates is equal with it. Same goes for the y. For shapeflag, you either put true or false. False makes it like the other hitTest, if the blue box that appears around a MC when you select hits the other ones blue box, the hitTest goes off. But if shapeflag is true, it only goes of if the area that hits isn’t empty. Sorry if that doesn’t make any sense.
That concludes this tutorial. Hopefully now you have some basic knowledge on AI.















haha first comment……..
Lol, the tutorials been up for less than 5 minutes and I have a comment already.
well it sorta doesnt help he lol cause i dont really need it
lol that came out wrong i mean the tutorial doesnt help me lol yer well ive like been on my computer all day and ur website is my only window lol so i just commented on it
Great! Now I can get started on ‘Astronaut 2′
*In kiddie voice* Your the best awesty!
How would I though make on the second frame of the camera (alarm frame) make it go to a ‘game over’ frame?
onClipEvent(enterFrame){ if(this.hitTest(_root.man._x, _root.man._y, true)){ _root.gotoAndStop(2); } }But you can change 2 to whatever frame the gameover frame is. Just replace the code on the camera with this.
Oh. I can’t believe I didn’t think of that. Thanks all the same.
Awesome, Thanks alot. I hope you make an AI tut with enemies atacking you and you attacking them, and with hp bars.
I’m making a game like that. It’s really hard! (to make)
im gunna have to agree with Daniel sept you should like combine abunch of tuts. on how to make a simple game
@Daniel: Have you taken the fighting game tutorial and/or the health system tutorial you should be able to figure that out.
@RedV: If you take the tutorials and understand them you should be able to do that yourself.
I AGREE WITH AWESTY
THNX 4 THE TUTE!
eek!
some thing gone wrong!
help!
ahh, its ok, i got it
wow! thnx, i made a very beautiful game
THANKS ALOT
what if u wanted to make a platformer/fighting game & u wanted the enemy to jump on the platforms to follow after u? instead of them just floating up after u, because that is easy enough…
Hey great tuto. I was wondering, what the code would be for a gaurd cycle ? ( i.e. a person walks a ditance stops for a time turns around and walks back). This would be soo much help for me. Please help
John
@KIM: … Thanks… o_O
@Guy: Well, you would have to make the enemy jump randomly, or when the character jumps or have something to trigger it. I might include that into another tutorial.
@John: The easiest way would be to make a MC with that in it on the first frame, and then on the second have it running so it could chase you or something.
Yes in the next one can you make how to might a fight game 2 and in it it says how to make locked charecters how to make the char selec screen plz
That would be a very long tutorial. I might make on later on down the track but I am not really interested in doing a tutorial that long at the moment. I have other stuff to do.
i think doing that tutorial is a very good idea…
I have an easy solution Darian. Make a title screen with a “play button” then make a second frame with pictures of all your characters. Convert the pictures to buttons. Make each button link to a different frame, with a background, and whamo! You have your self a character selection.
I would not know how to make locked characters. Maybe Google it?
With help of some of your recent tutorials, I have been able to come up with this:http://www.deviantart.com/deviation/44549623/ it’s a preview to a fighting game, a friend and I are making.
Nice work.
Lol, but a fake preloader? Preloaders are put there to make the wait less boring, or to show you how long it has left to load. Making a fake actually isn’t a very good idea, since it makes you wait even though you don’t really have to.
Yah… That’a how I started the idea though. I was throwing around some reloader ideas, and eventually came up with this. By the way I updated it now it has more features.
That could be a good tute. “Loading Screen”.
You could work on the walk cycle. When you walk, your legs dont go out in front of you, they stay underneath you. They also stay the same size
Also:
http://www.awestyproductions.c.....preloader/
I tried that, and it didn’t work. The preloader, that is.
Yah the walk cycle needs some work. My brother commented on that, but I never fixed it.
hey i making a game that the enemy follows you everywhere not just left and right….could you help me out???Your kind of looking down on everything and everything could move all around so i need the code for the enemy to follow you everywhere
thanks a billion
p.s. how could u put ur games on the internet so i could show people like u for example?
@Bob: It should work.
@Tim: Just this for the up and down:
if(this._y < (_root.man._y - 25)){
this._y = 3;
}
if(this._y > (_root.man._y 25)){
this._y -= 3;
}
}
That should work.
If you want to put your games on the internet, you could submit them to sites like Newgrounds, or make you own site.
http://www.newgrounds.com
Hmm. I’ll try it on something like an animation. Hehe I figured out that code you gave to Tim for my fly game awhile ago. well for the char. follower at least. You have taught me stuff!
lol, good to hear.
i posted on the last one can you read it and reply to me on this one and also mine doesnt work instead of changing color i want my enemy to attack but in meantime i will go at fight game so my character can lunge with his sword
For attacking, try my fight game tutorial. And put the attack code on the enemy where is changes color, so then it should attack instead of changing color if you do it right.
I added some script to the ‘Friend’ mc, now when u touch him u gain life (I also put in a hp bar). it doesnt spoil it though. u gain less than u lose.
Code 4 the ‘Friend’:
if(this.hitTest(_root.man)){
_root.hp += (random(3));
}
}
(you need to remove 1 of the curly bracket (}) befor you add it)
Cool idea.
how can i mkae the cameras shoot the ‘man’ when it gets close to it?
Kim, you just add the attacking code the camera’s light.
if(this.hitTest(_root.man)){
_root.hp -= (random(3) 1);
if(this.hitTest(_root.man)){
_root.hp -= (random(5) 1);
}
(I forgot a bracket)
if(this.hitTest(_root.man)){
_root.hp -= (random(3) 1);
}
(sorry, i keep messing up)
I know this sounds noobish (cause I am one…) and I’m only 13, but what would the script for jumping be? …yeah, not even AI jumping… just jumping… you have one on platform jumping, but I’m just looking for jumping.
I don’t understand why platform jumping wouldn’t work for any type of jumping that you would need.
@Jake: Platform jumping is the same thing, you can choose how many platforms you want.
do you mean just jumping? no platforms?
yes, no platforms
@Jake: The platforms aren’t necessary for the tutorial, just leave them out.
@Pamela: I have never made a tutorial like that. If you want me to make some tutorials just post here:
http://www.awestyproductions.c.....s/request/
Hey man!its awesome!
Hmm.. Good tutorial. I might need help with some things later, but for now I’m fine.
Nice job.
I have a question. How do you make the enemy follow you up and down, too?
if(this._y < (_root.man._y - 25)){
this._y += 3;
}
if(this._y > (_root.man._y 25)){
this._y -= 3;
}
}
I made my man MC and my enemy MC both have hp bars, and i put on my enemy that if it touches the man MC, mans hp goes down:
if(this.hitTest(_root.man)){
_root.hp -= (random(2) 1);
And on one of my frames in the man mc i made an attack and put:
onClipEvent(load){
if(this.hitTest(_root.enemy)){
_root.hp2 -= (random(3) 1);
But whenever i attack the enemy, its hp goes down but so does mine, because the enemy is told that when it touches ‘man’, mans hp goes down, and man is told that when it attacks enemy, enemy’s hp goes down.
So basiclly how can i make my man attack the enemy so his hp wont go down, since it touches the enemy.
I dont know if you got that
Try taking this tutorial:
http://www.awestyproductions.c.....ight-game/
I already took that tut and i know how to make it hit a standing thing, but i was talking about an AI enemy. I will just send you the .fla file of the game.
daniel, how about making a variable that is only true when you are fighting (in the fighting tute)
@Daniel something like this(may have sintax errors
in the first frame of the attacking mc
_root.man.fight=true;
in the enemy MC
if (this.hitTest(_root.man) && _root.man.fight==false)
manhp - you know it form now on
Hope this helps.
(Curse, I’m in dial-up conXion…)
Nice Tutorial! U explain everything well .
Thank u very,very,very much.
GREAT tutorial colud try and make it continue maybe when you walk into the enemy he attacks you. And your healt bar goes down.
There are already tutorials for that.
awesty could you make a tutorial on how to make the enemy run when chasing you and when standing still to sit there
I have shown how to do that in the AI tutorials.
i am using this code:
onClipEvent(enterFrame){
if(this._x (_root.man._x + 25)){
this._x -= 3;
}
}
my character follows the other character but goes to the othe side of the scrren and stays that distants apart
Uh…
What is this supposed to do?
this._x (_root.man._x + 25)im making a fighting game, and how do u make the enemy turn around an face you if you go past him… not just walk backwards?