Hello there.
Im strugling for a few hours now to get this code to work in actionscript 3.
And i cant seem to succed, most probably its because im a newby and the code also might sound stupid.
The game im trying to create is a platform game.
So what i want to do is make enemy patrol between 2 points (400 and 750 you'll see these points in my code) and if the hero player (named circle in my code)
reaches a certain distance from the enemy, i want enemy to follow him (enemy speed will increase while following).
At the moment with my code the enemy speed will just increase but still patroll betwen those points no matter the distance.
This is the code:
Actionscript Code:
package
{
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.DisplayObject;
    import flash.events.*;
 
    public class Enemy1 extends MovieClip
    {
        //_root will signify the root of the document
        private var _root:Object;
        //how quickly the enemy can move
        private var speed:Number;
        //-1 is left, 1 is right
        private var direction:int;
        private var core:Object;
        private var yd:Number;
        private var xd:Number;
        private var gx:Number = 0;
        private var gy:Number = 0;
        private var gravity:Number = 1;
        private var target:Object;




 
        public function Enemy1()
        {
            //this code will only be run once
            addEventListener(Event.ADDED, beginClass);
            //this code will constantly be run
            addEventListener(Event.ENTER_FRAME, eFrame);

        }

        private function beginClass(event:Event):void
        {
            this.x=750;
            _root = MovieClip(root);
            removeEventListener(Event.ADDED, beginClass);
        }
 
        private function eFrame(event:Event):void
        {
            var distance = Math.sqrt( ( this.x - _root.circle.x ) * ( this.x - _root.circle.x ) + ( this.y - _root.circle.y ) * ( this.y - _root.circle.y ) );
            this.x += speed * direction;
            if(distance<300 && direction==-1)
            {
                speed=8;
                direction=-1;
                this.scaleX=7/10;
                this.x--;
            }
            else if(distance<300 && direction==1)
            {
                speed=8;
                direction=1;
                this.scaleX=-7/10;
                this.x++;
            }
            else
            {
                if(this.x>750 && !distance<50)
                {
                    speed=5;
                    direction=-1;
                    this.scaleX=7/10;
                    gotoAndStop(1);
                }
                if(this.x<400 && !distance<50)
                {
                    speed=5;
                    direction=1;
                    this.scaleX=-7/10;
                    gotoAndStop(1);
                }
            }

            if(!this.hitTestObject(_root.platform))        
            {
                gy += gravity;
                this.x += gx;
                this.y += gy;
            }
        }
    }
}

Thanks in advance.