0

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.

Edited by kx220

1
Contributor
1
Reply
5
Views
5 Years
Discussion Span
Last Post by kx220
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.