PDA

View Full Version : Enemy AI Help...



Doug89
July 12th, 2005, 02:55 AM
Hello there guys, im new here. I have been playing around with flash for a few years now just for fun and have tried extensively to make games but when i cant figure something out i usually just abandon the project and quit. Well i want to follow through for once and i was wondering if you guys could help.

Ok, heres the dilemma. I am making a Helicopter Shooting Game type thing with Flash 5. The game works like this. You start on a small helipad and takeoff and start flying right across the screen. when you get about 1/3 across the screen the helicopter stops moving and the terrain below you (in this case grass) starts to scroll and the enemy helicopter movieclip flies in. It cant actually touch you but it stays about another 1/3 of the stage away and follows your movements up and down and a random selected speed. The problem is i cant get the clip to know when to fire at the player. I first thought it was my shootbullet function or my movebullet function or something. I then decided to scrap the whole shootbullet, etc. and start fresh. Thats when i learned after repeated tests that the computer didn;t know when to fire. The code i was using wasnt working. I want the computer to execute the contents of its if() statement when it is within 10 pixels either way of the players helicopter. I have tried everything i can think of but i cant get it to work. Any help would be greatly appreciated! Thanks guys!


-Doug

Blackspirit
July 12th, 2005, 04:07 AM
Welcome to Kirupa :D

I did a game a couple of years back, top down shooter. I had a boss character the scrolled across the top of the screen, and shot whenever the player was around the same x value as him.

It sounds like your doing the right thing, all you need is a simple if statement.

If(boss._x-10 > player._x && boss._x + 10 < player._x)

that checks if a player is in a 10 pixel range of the bosses x value.

Doug89
July 14th, 2005, 06:55 PM
Thanks But i still cant get it to work perhaps there is another error in my code. Heres the code for the enemy helicopter movieclip. Where it says if(some stuff){ this.gotoAndPlay(2)}
it will say something like shootBullet() when i can get it working the clip has 2 frames on of just the helicopter and when the helicopter is in firiing range of the player it says IT WORKS above the helicopter. Unfortunately that message is not appearing when i run this code. Here it is:


// setup starting position
onClipEvent (load) {
function reset () {
this._x = 600;
this._y = random(200)+100;
enemySpeed = random(4)+1;
enemyFollowSpeed = random(2)+0.5;
this.gotoAndStop(1);
}
reset();
}
onClipEvent (enterFrame) {
// movement onto screen
if (this._x>=350) {
if (_root.helicopter.scrollStart) {
this._x -= enemySpeed+_root.mainGround.groundSpeed;
} else {
this._x -= enemySpeed;
}
}
// Enemy AI
// follow helicopter clip
if (this._y<_root.helicopter._y) {
this._y += enemyFollowSpeed;
}
if (this._y>_root.helicopter._y) {
this._y -= enemyFollowSpeed;
}
// if near helicopter then fire
if (this._y-10>_root.helicopter._y and this._y+10<_root.helicopter._y) {
this.gotoAndPlay(2);
} else {
this.gotoAndPlay(1);
}
}

Blackspirit
July 15th, 2005, 03:38 AM
Ok, my logic was a bit stuffed, sorry. :D

This will work me thinks...

if (this._y-10<_root.helicopter._y && this._y+10>_root.helicopter._y)

I got the greater than and smaller then signs mixed up :P

Doug89
July 15th, 2005, 08:24 PM
Great Thanks a Bunch! Ill post my game up when i get it ready thanks for all your help!

optixburn
July 19th, 2005, 12:08 PM
Ok, my logic was a bit stuffed, sorry. :D

This will work me thinks...

if (this._y-10<_root.helicopter._y && this._y+10>_root.helicopter._y)

I got the greater than and smaller then signs mixed up :P

if you can call the Math.abs() function you don't have to worry about so many greater than and less than sign

Ex

if(Math.abs(this._y-_root.helicopter._y) < 10)

explaination

Math.abs(this._x - _root.helicopter._y) :
gives back a positive number for hte difference between the user y position and the enemy's y position

< 10 :
this allows you to set a y block of how many pixels to detect the player ...

this code give the enemy a "line of sight" of 10 pixels while the previous code gives it a "line of sight" of 20

don't know if this helps much, but just so you guys have another trick in your bags

Blackspirit
July 20th, 2005, 02:26 AM
Wow, never thought to use Math.abs for that. Good call optix. I'll have to remember that. :D