Hi, I am creating a sort of zombie game. The concept is if the player gets too close within a distance, the zombie will chase the player. There will be a hit detection when the zombie object hits the player object. I used a trace statement for when it hits. The player/ zombie moves on the x-axis. This is what I have so far.

My problem is I can move the player but when I get close to the zombie, the zombie doesn't chase the player. What works is when the player touches the zombie, the trace statement works.

my code:

package  {
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.events.Event;

    public class DistanceBaseCollision extends MovieClip {


        private var leftArrow:Boolean = false;
        private var rightArrow:Boolean = false;     
        private var speed:uint=5;

        public function DistanceBaseCollision() {
            // constructor code
            init();
        }

        private function init():void {

            stage.addEventListener (KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener (KeyboardEvent.KEY_UP, onKeyUp);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function onKeyDown(event:KeyboardEvent):void {

            switch (event.keyCode) {
                case Keyboard.RIGHT :
                rightArrow=true;
                player.x += speed;
                break;
                case Keyboard.LEFT :
                leftArrow=true;
                player.x -= speed;
                break;
            }
        }

        private function onKeyUp (event:KeyboardEvent):void {
            switch (event.keyCode) {
                case Keyboard.RIGHT :
                rightArrow=false;
                break;
                case Keyboard.LEFT :
                leftArrow=false;
                break;
            }
        }

        private function onEnterFrame(event: Event):void {


            var dx:Number = player.x - zombie.x;
            var dy:Number = zombie.y - player.y;
            var dist:Number = Math.sqrt(dx*dx+dy*dy);


            if(dist < player.width/2 + zombie.width/2 && zombie.height - player.height) {
                trace("hit");
            }
        }

    }

}

Please help. Thank you in advance.

Dani AI

Generated

The brief problem description and the code in the first post make the likely causes obvious: the distance test is malformed and there’s no explicit “move the zombie toward the player” step. later reported the issue was solved; below is a focused explanation of the two common mistakes and a small, robust onEnterFrame pattern that will reliably produce chasing behavior and clear collision detection.

Two quick points to check:

  • The boolean expression && zombie.height - player.height does not do what you expect in AS3. A numeric expression coerces to Boolean (0 is false, other numbers are true), so that second operand almost always evaluates true. For a circular/axis‑aligned distance check you only need to compare the computed distance to the sum of the radii.
  • If you want the zombie to chase, you must update the zombie’s position each frame when the player is inside the chase radius. A stable approach is to compute the vector from zombie to player, normalize it, then add the normalized vector times speed to zombie position. Guard against division by zero and separate the “chase” radius from the smaller “collision” radius to avoid jitter.

A compact AS3 example (put this inside your enterFrame handler):

var dx:Number = player.x - zombie.x;
var dy:Number = player.y - zombie.y;
var dist:Number = Math.sqrt(dx*dx + dy*dy);

var collisionR:Number = (player.width + zombie.width) * 0.5;
var chaseR:Number = 200; // choose as needed
var speed:Number = 3;

if (dist < chaseR && dist > collisionR) {
    var inv:Number = 1 / Math.max(dist, 1);
    zombie.x += dx * inv * speed;
    zombie.y += dy * inv * speed;
} else if (dist <= collisionR) {
    trace("hit");
}

Extra tips: set movement flags in keyDown/keyUp and move the player inside enterFrame for smooth input; pick chaseR noticeably larger than collisionR to prevent flicker; use a frame time multiplier (delta time) if you need consistent speeds across different frame rates; and check registration points of your clips—width/height based radii assume the registration point and bounding boxes match the visual sprite.

Hey, I solved the problem already.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.