1

I'm working on a game project, and I am working on the AI aspect of the game. I want the enemy objects to start aiming and shooting at the player when they are in sight of the enemy, and i came across this article on a way to do that: http://www.emanueleferonato.com/2007/04/29/create-a-flash-game-like-security-part-2/

my question is can you do this same thing without using an actual line? can you for instance use a hit test point and basically define a line? or some other way of doing it without actually putting an object on the stage.

I am trying to make things as efficient as possible and don't want to use this approach if possible. if you have some advice, or code or links to a helpful resource I would greatly appreciate that!

AlexW.H.B.
  • 1,781
  • 3
  • 25
  • 50

1 Answers1

3

Just use the calculations, but not the line

This is the important code

dist_x = _root.hero._x-_x;
dist_y = _root.hero._y-_y;
dist = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
angle = Math.atan(dist_y/dist_x)/(Math.PI/180);
if (dist_x<0) {
    angle += 180;
}
if (dist_x>=0 && dist_y<0) {
    angle += 360;
}
wall_collision = 0;
for (x=1; x<=dist; x++) {
    point_x = _x+x*Math.cos(angle*Math.PI/180);
    point_y = _y+x*Math.sin(angle*Math.PI/180);
    if (_root.wall.hitTest(point_x, point_y, true)) {
        wall_collision = 100;
        break;
    }
}

if wall_collision = 100, the player is in sight of the cop. I'd just use a Boolean for this though.

Creynders
  • 4,573
  • 20
  • 21
  • awesome thank you! this helps so much. :) and i would have to agree with you on the Boolean. i'm not sure why it is written that way. – AlexW.H.B. Oct 01 '11 at 09:42
  • he uses it as the alpha value for the line, that's why. BTW, you do know this is written in AS2 though? – Creynders Oct 01 '11 at 10:11
  • oh that's what it is. :) and ya i knew it was AS2 by all of the underscores infront of the properties. I am writing my game in as3 though. would the hitest method sill work for this purpose? i know that the new hittest only tests vs. bounding boxes. – AlexW.H.B. Oct 02 '11 at 04:45
  • Oh i just took a closer look at the code that is actually a very interesting way of solving this. thank you. :) – AlexW.H.B. Oct 03 '11 at 10:53