Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

AS3 Collision Detection

Name: Baird !ATI.GearS. 2012-12-18 0:37

I'm working on a game in AS3 for my Programming IV class. Right now I specifically am developing basic enemy AI. I figured first thing first, I need to check that the enemy has Line of Sight.

I'm drawing a line between the enemy and the play and checking to see if anything collides with the wall.

Flash has Object.hitTestObject(Object) which returns a boolean, but I can't use this because it doesn't use the actual graphic, it uses a bounding box that is a square around the object.

Does anyone know of a quick and simple implementation I can use?

Here's my NPC code that Enemy will eventually extend:

[code]
package  {
   
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.display.Stage;
    import flash.display.Shape;
   
   
    public class NPC extends Character {
        var target:Character = null;
        var screen:Stage = null;
        var l:Shape = new Shape();
        var wall:Wall = null;
       
        public function NPC(s:Stage,g:Game,X:int,Y:int) {
            super(s,g,"NPC",X,Y);
           
            target = g.playerOne;
            wall = g.wall;
            screen = s;
            s.addEventListener(Event.ENTER_FRAME, loop);
        }
       
        public function loop(Event){
                canSeeTarget();
        }
       
        public function canSeeTarget(){
            l.graphics.clear();
            screen.addChild(l);
           
            l.graphics.lineStyle(2,009900,1);
            l.graphics.moveTo(target.x,target.y);
            l.graphics.lineTo(this.x,this.y)
           
            var wall_collision = 0;
            for (var i=1; i<=dist; i++) {
                var point_x = (x+_root.cop_patrol._x)+x*Math.cos(angle*Math.PI/180);
                point_y = (_y+_root.cop_patrol._y)+x*Math.sin(angle*Math.PI/180);
                if (_root.wall.hitTest(point_x, point_y, true)) {
                    wall_collision = 100;
                    break;
                }
            }
        }
    }
}

Name: Baird !ATI.GearS. 2012-12-18 0:38

>/g/ has code tags
>/prog/ doesn't
Okay.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List