package {
        import flash.display.MovieClip;
        import flash.text.TextField;
        import flash.display.Shape;
        import flash.events.Event;
 
        public class Avoider extends MovieClip {
                public var target:MovieClip = new MovieClip;
                public var player:MovieClip = new MovieClip;
                public var score:int = 0;
                public var mouseDiffX:int = 0;
                public var mouseDiffY:int = 0;
 
                public function Avoider() {
                        addEventListener(Event.ENTER_FRAME, mouseMovement);
                        drawPlayer();
                        drawNewTarget();
                }
                public function drawNewTarget() {
                        var posX:uint = randRange(30,520);
                        var posY:uint = randRange(30,370);
                        target.graphics.beginFill(0x0000FF);
                        target.graphics.drawRect(0, 0, 20, 20);
                        target.x = posX;
                        target.y = posY;
                        target.graphics.endFill();
                        addChild(target);
                        target.addEventListener(Event.ENTER_FRAME,gotToTarget);
                }
                public function drawPlayer() {
                        player.graphics.beginFill(0x000000);
                        player.graphics.drawCircle(0, 0, 10);
                        player.x = 200;
                        player.y = 200;
                        player.graphics.endFill();
                        addChild(player);
                }
                private function randRange(start:Number,end:Number):Number {
                        return Math.floor(start + Math.random() * (end - start));
                }
                private function gotToTarget(e:Event) {
                        if (e.target.hitTestObject(player)) {
                                e.target.x = randRange(30,520);
                                e.target.y = randRange(30,370);
                                drawNewEnemy();
                                score++;
                                scoretext.text = "Score: " + String((score * 10));
                        }
                }
                public function mouseMovement(event:Event):void {
                        mouseDiffY = mouseY - player.y;
                        mouseDiffX = mouseX - player.x;
 
                        if (mouseDiffY < 0) {
                                if (!player.hitTestObject(walltop)) {
                                        player.y += mouseDiffY / 6;
                                }
                        }
                        if (mouseDiffY > 0) {
                                if (!player.hitTestObject(wallbottom)) {
                                        player.y += mouseDiffY / 6;
                                }
                        }
                        if (mouseDiffX < 0) {
                                if (!player.hitTestObject(wallleft)) {
                                        player.x += mouseDiffX / 6;
                                }
                        }
                        if (mouseDiffX > 0) {
                                if (!player.hitTestObject(wallright)) {
                                        player.x += mouseDiffX / 6;
                                }
                        }
                }
        }
}