1. 44
posts
Registered User

## AI Enemies

i am trying to make a game where the bad guys randomly walk about in a certain area. the enemies will attack you when you enter into there roaming around space. i have been working in flash for 4 years and cant think how to do it

thanks
Stupid Saint

2. Code:
```if(Math.abs(_root.hero._x-_root.enemy._x)<100 && Math.abs(_root.hero._y-_root.enemy._y)<100){
//enemy attacks
}```
It's pretty much saying that if your hero's x and y positions are less than 100 pixels away from your enemies x and y positions, then your enemy should attack. You can change his attack radius by altering the distance of the x and y.

Note: To make sure this works correctly, your registration point must be in the exact same place in all movieclips.

Hope that helped!
Last edited by Skribble; May 14th, 2008 at 09:33 PM.

3. Originally Posted by stupidsaint
i am trying to make a game where the bad guys randomly walk about in a certain area. the enemies will attack you when you enter into there roaming around space. i have been working in flash for 4 years and cant think how to do it

thanks
Stupid Saint
How can you not figure this out if you have been doing flash for 4 years? I guess you spend most of that time on animation then XD

Anyways, for the moving part a simple solution would be: would let every enemy pick a random location in the field, move there, and when it has reached that destination, repeat.

Anyways, the moving part could be simple solution. Let every enemy pick a location, let every enemy move to that location, when an enemy reaches that location: repeat.

Code:
```enemy.moveToLocation = new Point (getRandomNumber(0, Stage.Width), getRandomNumber (0, Stage.Height));

enemy.OnEnterFrame = function ()
{
if (enemy.X > moveToLocation.X) { enemy.X -= enemy.movingSpeed; }
else { enemy.X += enemy.movingSpeed; }
// continue

if (getDistance (enemy.X, enemy.Y, moveToLocation.X, moveToLocation.Y) < movingSpeed)
{
enemy.moveToLocation = new Point (getRandomNumber(0, Stage.Width), getRandomNumber (0, Stage.Height));
}
}
getRandomNumber(min:Number, max:Number):Number
{
return (Math.floor(Math.random()*(max-min+1)) + min);
}
getDistance (x1:Number, x2:Number, x2:Number, y2:Number):Number
{
var xd = x1 - x2;
var yd = y1 - y2;
return ((xd * xd) + (yd * yd))^0.5;
}```
The above code was quickly typed to give you an idea on how you could be doing this. Though, you may not want to use the geom classes for point, but rather create your own one. And also, there are better ways to do this

4. Originally Posted by ArmoredSandwich
Code:
```getDistance (x1:Number, x2:Number, x2:Number, y2:Number):Number
{
var xd = x1 - x2;
var yd = y1 - y2;
return ((xd * xd) + (yd * yd))^0.5;
}```
I just notied ((xd * xd) + (yd * yd))^0.5; line. Is ^0.5 actually valid code for 'power'? For some reason I have been using Math.sqrt...

5. 44
posts
Registered User
Originally Posted by ArmoredSandwich
How can you not figure this out if you have been doing flash for 4 years? I guess you spend most of that time on animation then XD

Anyways, for the moving part a simple solution would be: would let every enemy pick a random location in the field, move there, and when it has reached that destination, repeat.

Anyways, the moving part could be simple solution. Let every enemy pick a location, let every enemy move to that location, when an enemy reaches that location: repeat.

Code:
```enemy.moveToLocation = new Point (getRandomNumber(0, Stage.Width), getRandomNumber (0, Stage.Height));

enemy.OnEnterFrame = function ()
{
if (enemy.X > moveToLocation.X) { enemy.X -= enemy.movingSpeed; }
else { enemy.X += enemy.movingSpeed; }
// continue

if (getDistance (enemy.X, enemy.Y, moveToLocation.X, moveToLocation.Y) < movingSpeed)
{
enemy.moveToLocation = new Point (getRandomNumber(0, Stage.Width), getRandomNumber (0, Stage.Height));
}
}
getRandomNumber(min:Number, max:Number):Number
{
return (Math.floor(Math.random()*(max-min+1)) + min);
}
getDistance (x1:Number, x2:Number, x2:Number, y2:Number):Number
{
var xd = x1 - x2;
var yd = y1 - y2;
return ((xd * xd) + (yd * yd))^0.5;
}```
The above code was quickly typed to give you an idea on how you could be doing this. Though, you may not want to use the geom classes for point, but rather create your own one. And also, there are better ways to do this
i have spent the first year on learning the basics in flash drawing and little actionscript

the second year i continued to make images and develop movies

the third year i started to learn the slightly more advanced actionscript

the fourth year i started to learn amazingly advanced actionscript, xml (reading and writing) and asp loading. (i am only 14 so i'm not very good at leaning anyway :-D)

Stupid Saint

6. Originally Posted by misterooga
I just notied ((xd * xd) + (yd * yd))^0.5; line. Is ^0.5 actually valid code for 'power'? For some reason I have been using Math.sqrt...
Yea it's the same thing, square root is the same thing as ^0.5.

And my code would work if you just fixed the typos and completed it. As I said it was to give you a basic idea on how it would work. If you can't handle things like that, that's fine but don't expect complete codes laid out for you.

7. Hey,
Stupid saint, could you create a MC and animate it walking around? Then when your character hitTest's the MC, the MC shoots him? Just an idea

8. Well...

This is not a good AI, in my opinion, but, since this is what you want, here's a .fla file that does what I think you're looking for. Note that attributes like enemy speed and sight range can be set differently per enemy, if you prefer, but, since this is supposed to be a quickie and simple example, I used global values for all enemies (and also random positioning for them and the player). Also, the "sight" range is a square, you can do it a circle if you take the time and trigonometry to do so, but I'm not in the mood right now...

The enemies "attack", in this example, consists only in following the player and getting a nastier look. They don't actually affect the playert ijn any way if they happen to catch him.

Besides that, I'd strongly suggest you try to devise a better behavior. All this random patrolling doesn't sound good to me.

There are currently 1 users browsing this thread. (0 members and 1 guests)