3 Replies Latest reply on Feb 15, 2011 1:44 PM by gherome_gherome

    Writing a Class for making the 'enemies' follow the 'player'...

    gherome_gherome

      Greetings;

       

      I'm an actionscript beginner and trying  to write a class to  make the 'enemies' follow the 'player' (shown below). I'm trying to write it such that it takes two MovieClips as arguments, and moves the first one toward the second one. When I import the Class try to use it in Main.as by typing  " var followPlayer:FollowPlayer = new FollowPlayer(fishMov0, player);", I get the following error:

       

      "ArgumentError: Error #1063: Argument count mismatch on jab.enemy::FollowPlayer/followPlayer(). Expected 2, got 1."

       

       

      Any ideas/suggestions would be appreciated.

       

      Thanks,

       

      Jeremy

       

       

       

       

       

      package  jab.enemy
      {
          import flash.display.MovieClip;
          import flash.events.Event;
          public class FollowPlayer
          {
              private var _clip1:MovieClip
              private var _clip2:MovieClip
              private var speed:uint = 1.5;
              public function FollowPlayer(clip1:MovieClip, clip2:MovieClip): void
              // clip1 is the enemy, clip2 the player being followed.
              {
                  _clip1 = clip1;
                  _clip2 = clip2;
                  _clip1.stage.addEventListener(Event.ENTER_FRAME, followPlayer);
                  _clip2.stage.addEventListener(Event.ENTER_FRAME, followPlayer);
              }
             
              private function followPlayer(_clip1:MovieClip, _clip2:MovieClip)
              {
                      if (_clip1.y > _clip2.y)
                      {
                          _clip1.y -= speed;
                      }
                      if (_clip1.y < _clip2.y)
                      {
                          _clip1.y += speed;
                      }
                      if (_clip1.x > _clip2.x)
                      {
                          _clip1.x -= speed;
                      }
                      if (_clip1.x > _clip2.x)
                      {
                          _clip1.x -= speed;
                      }
              }

       

          }
         
      }

        • 1. Re: Writing a Class for making the 'enemies' follow the 'player'...
          kglad-zqbLf4 Level 3

          followPlayer() is being sent one parameter, not two and, that parameter is an event.

          1 person found this helpful
          • 2. Re: Writing a Class for making the 'enemies' follow the 'player'...
            moccamaximum Level 5

            another thing

             

            you can`t write

             

            _clip1.stage.addEventListener(Event.ENTER_FRAME, followPlayer);

            _clip2.stage.addEventListener(Event.ENTER_FRAME, followPlayer);

             

            no movieclip can parent the stage

             

            and you have two functions with the same name

            1 person found this helpful
            • 3. Re: Writing a Class for making the 'enemies' follow the 'player'...
              gherome_gherome Level 1

              Thanks guys! My code was seriously flawed, but I ended up getting it to work. For anyone else that might be interested, see 'FollowPlayer' below, an extremely simple class which takes two MovieClips as arguments, and moves the first one slowly (at 24fps) toward the second one:

               

               

               

              package  jab.enemy
              {
                  import flash.display.MovieClip;
                  import flash.events.Event;
                  public class FollowPlayer
                  {
                      private var _clip1:MovieClip
                      private var _clip2:MovieClip
                      private var speed = 1 + Math.random();
                      public function FollowPlayer(clip1:MovieClip, clip2:MovieClip): void
                      // clip1 is the enemy, clip2 the player being followed.
                      {
                          _clip1 = clip1;
                          _clip2 = clip2;
                          _clip1.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame)
                          function onEnterFrame(event:Event):void
                     
                          {
                             
                             
                              if (_clip1.y > _clip2.y)
                              {
                                  _clip1.y -= speed;
                              }
                              if (_clip1.y < _clip2.y)
                              {
                                  _clip1.y += speed;
                              }
                              if (_clip1.x > _clip2.x)
                              {
                                  _clip1.x -= speed;
                              }
                              if (_clip1.x < _clip2.x)
                              {
                                  _clip1.x += speed;
                              }
                          }
                             
                      }