开发者

AS3 line of sight without using hittest object.

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜