Best viewed in Firefox

Awesty Productions

Basic Artificial Intelligence Part 2

November 30th, 2006 by awesty

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.

Here is an example. (link)

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:

onClipEvent(enterFrame){
    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:

onClipEvent(enterFrame){
    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. ;)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed

67 Comments »

Comment by joey
2006-11-30 12:07:27

haha first comment……..

 
Comment by awesty
2006-11-30 12:09:27

Lol, the tutorials been up for less than 5 minutes and I have a comment already. :D

 
Comment by joey
2006-11-30 12:19:50

well it sorta doesnt help he lol cause i dont really need it

 
Comment by joey
2006-11-30 13:02:47

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

 
Comment by Bob
2006-11-30 22:03:33

Great! Now I can get started on ‘Astronaut 2′

*In kiddie voice* Your the best awesty!

 
Comment by Bob
2006-11-30 22:06:57

How would I though make on the second frame of the camera (alarm frame) make it go to a ‘game over’ frame?

 
Comment by awesty
2006-12-01 07:36:13
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.

 
Comment by Bob
2006-12-01 07:43:13

Oh. I can’t believe I didn’t think of that. Thanks all the same.

 
Comment by Daniel
2006-12-06 11:26:35

Awesome, Thanks alot. I hope you make an AI tut with enemies atacking you and you attacking them, and with hp bars.

Comment by Jeffrey
2007-11-04 02:18:30

I’m making a game like that. It’s really hard! (to make)

 
 
Comment by RedV
2006-12-07 04:48:37

im gunna have to agree with Daniel sept you should like combine abunch of tuts. on how to make a simple game

 
Comment by awesty
2006-12-07 15:13:57

@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. ;)

 
Comment by KIM
2006-12-09 17:25:16

I AGREE WITH AWESTY

THNX 4 THE TUTE!

 
Comment by KIM
2006-12-09 17:36:10

eek!
some thing gone wrong!
help!

 
Comment by KIM
2006-12-09 17:52:24

ahh, its ok, i got it

 
Comment by KIM
2006-12-09 19:43:20

wow! thnx, i made a very beautiful game

THANKS ALOT

 
Comment by guy
2006-12-10 05:50:05

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…

 
Comment by John
2006-12-10 06:40:03

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

 
Comment by awesty
2006-12-10 11:14:29

@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.

 
Comment by Darin
2006-12-11 06:16:57

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

 
Comment by awesty
2006-12-11 15:42:52

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.

Comment by Izzy Subscribed to comments via email
2007-08-31 09:03:38

i think doing that tutorial is a very good idea…

 
 
Comment by Bob
2006-12-11 22:04:55

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?

 
Comment by Bob
2006-12-12 09:02:52

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.

 
Comment by awesty
2006-12-12 15:37:39

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.

 
Comment by Bob
2006-12-13 11:18:53

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.

 
Comment by Bob
2006-12-13 11:42:06

That could be a good tute. “Loading Screen”.

 
Comment by awesty
2006-12-13 17:00:00

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 :P
Also:

http://www.awestyproductions.c.....preloader/

 
Comment by Bob
2006-12-14 07:58:34

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.

 
Comment by Tim
2006-12-15 06:38:49

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

 
Comment by Tim
2006-12-15 06:43:49

p.s. how could u put ur games on the internet so i could show people like u for example?

 
Comment by awesty
2006-12-15 12:05:08

@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

 
Comment by Bob
2006-12-15 12:07:40

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!

 
Comment by awesty
2006-12-15 12:11:23

lol, good to hear.

 
Comment by jesty
2006-12-17 19:42:42

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

 
Comment by awesty
2006-12-18 10:59:21

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.

 
Comment by none
2006-12-23 10:47:55

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)

 
Comment by awesty
2006-12-23 12:20:02

Cool idea.

 
Comment by KIM
2006-12-24 10:48:10

how can i mkae the cameras shoot the ‘man’ when it gets close to it?

 
Comment by none
2006-12-25 03:46:11

Kim, you just add the attacking code the camera’s light.

 
Comment by none
2006-12-25 03:48:50

if(this.hitTest(_root.man)){
_root.hp -= (random(3) 1);

 
Comment by none
2006-12-25 03:49:31

if(this.hitTest(_root.man)){
_root.hp -= (random(5) 1);
}

(I forgot a bracket)

 
Comment by none
2006-12-25 04:12:42

if(this.hitTest(_root.man)){
_root.hp -= (random(3) 1);
}

(sorry, i keep messing up)

 
Comment by Jake
2006-12-28 06:41:51

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.

 
Comment by Bulletluigi
2006-12-28 12:20:38

I don’t understand why platform jumping wouldn’t work for any type of jumping that you would need.

 
Comment by awesty
2006-12-28 17:55:43

@Jake: Platform jumping is the same thing, you can choose how many platforms you want.

 
Comment by KatyS
2006-12-31 09:07:28

do you mean just jumping? no platforms?

 
Comment by Jake
2006-12-31 11:08:33

yes, no platforms

 
Comment by Pamela
2006-12-31 12:55:58

:P Hi, I’m 12 years old and have 68 websites (100% True) and they arew mostly gaming websites where i showoff games that I have made. Anyway, I remember a tutorial that you (awesty) made. It was where the character clicked a button and then it made some stats appear liek karma, strength, money and wisdom. I am making a new game and forgot where the tutorial is. You can contact me at http://pamelahoward.deviantart.com/

 
Comment by awesty
2006-12-31 13:03:43

@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/

 
Comment by arvind rangesh
2007-01-02 17:28:43

Hey man!its awesome!

 
Comment by burningsun
2007-01-03 10:10:57

Hmm.. Good tutorial. I might need help with some things later, but for now I’m fine.

Nice job.

 
Comment by burningsun
2007-01-03 10:15:11

I have a question. How do you make the enemy follow you up and down, too?

 
Comment by awesty
2007-01-03 11:20:00

if(this._y < (_root.man._y - 25)){
this._y += 3;
}
if(this._y > (_root.man._y 25)){
this._y -= 3;
}
}

 
Comment by Daniel
2007-01-08 06:06:04

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 :D

 
Comment by awesty
2007-01-13 15:58:42
 
Comment by Daniel
2007-01-14 11:13:24

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.

 
Comment by KIM
2007-01-23 13:21:16

daniel, how about making a variable that is only true when you are fighting (in the fighting tute)

 
Comment by Grifo
2007-02-16 08:30:12

@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…)

 
Comment by Amar
2007-03-18 23:07:01

Nice Tutorial! U explain everything well .
Thank u very,very,very much.

 
Comment by Moulay Subscribed to comments via email
2007-05-28 17:45:07

GREAT tutorial colud try and make it continue maybe when you walk into the enemy he attacks you. And your healt bar goes down.

Comment by awesty
2007-07-03 18:06:46

There are already tutorials for that.

Comment by Izzy Subscribed to comments via email
2007-08-31 09:15:17

awesty could you make a tutorial on how to make the enemy run when chasing you and when standing still to sit there

Comment by awesty
2007-09-10 17:19:09

I have shown how to do that in the AI tutorials.

 
 
 
 
Comment by simon Subscribed to comments via email
2007-08-11 02:54:12

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

Comment by awesty
2007-09-10 17:20:26

Uh…

What is this supposed to do?

this._x (_root.man._x + 25)

 
 
Comment by ike
2008-04-26 11:01:12

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?

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.