1 Reply Latest reply on Jun 21, 2014 11:55 AM by kglad

    Enemy AI (Making enemy follow player)

    DeathAngle Level 1

      Hi all, I have been trying to add basic Enemy AI to my game where the enemy follows the player. Any idea of how I could do this? The main character has an instance name of "player" and here is my Enemy class. Thankyou in advance

       

       

       

      package  {
              import flash.display.MovieClip;
              import flash.events.Event;

              public class Enemy extends MovieClip {
               private var xSpeedConst:int = 2;
         private var flip:int = 1;
                  public function Enemy(xLocation:int, yLocation:int) {
                   // constructor code
                      x = xLocation;
                      y = yLocation;
                      addEventListener(Event.ENTER_FRAME, loop);
                  }

                  public function loop(e:Event):void {
         
                     if ((flip%2) == 1){
           x += xSpeedConst;
          }else if((flip%2) == 0){
           x += (-xSpeedConst);
          }
                  }

                  public function removeSelf():void {
                      trace("remove enemy");
                      removeEventListener(Event.ENTER_FRAME, loop);
                      this.parent.removeChild(this);
                  }

         public function changeDirection():void{
                   trace("x ="+x);
                   flip++;
                  }
             
      }
      }

        • 1. Re: Enemy AI (Making enemy follow player)
          kglad Adobe Community Professional & MVP

          if player has the same parent an your enemies, use:

           

           

          package  {
                  import flash.display.MovieClip;
                  import flash.events.Event;

                  public class Enemy extends MovieClip {
                   private var xSpeedConst:int = 2;
             private var flip:int = 1;

          private var angle:Number;
                      public function Enemy(xLocation:int, yLocation:int) {
                       // constructor code
                          x = xLocation;
                          y = yLocation;
                      this.addEventListener(Event.ADDED_TO_STAGE,init);
                      }

          private function init(e:Event):void{

              this.addEventListener(Event.ENTER_FRAME, loop);

          }

                      private function loop(e:Event):void {
             angle=Math.atan2(this.parent.player.y-this.y,this.parent.player.x-this.x);
          this.x+=xSpeedConst*Math.cos(angle);

          this.y+=xSpeedConst*Math.sin(angle);

          // i'm not sure what your flip variable is all about, but usually you'd use something like

          if(this.parent.player.x>this.x){

          this.scaleX=1;  // or vice-versa

          } else {

          this.scaleX=-1;

          }
                      }

                      public function removeSelf():void {
                          trace("remove enemy");
                          this.removeEventListener(Event.ENTER_FRAME, loop);
                          this.parent.removeChild(this);
                      }

             public function changeDirection():void{
                       trace("x ="+x);
                       flip++;
                      }
                 
          }
          }

          1 person found this helpful