title: AS3. How to make good collision between hero's and enemy's animations?
body: <p>What is best way to make collision between player and enemy? </p>
  
  <p>In stay animation my Player's width is 30px, in walking animation 40px and in attack1 animation width 60px. </p>
  
  <p>For now I use code:</p>
  
  <pre><code>if (Enemy.hitTestObject(Hero))
              {
      Enemy.gotoAndStop("attack1");
      }
  </code></pre>
  
  <p>If Enemy touch Hero (main character) starts attack animation. But here is problem that if player's current state is in attack position Enemy stay so far ant trying to hit. </p>
  
  <p>I need to make something like that enemy always checked player's "stay" animations width, not current animation. Or maybe you could suggest better collision tips? Thank you.</p>
  
  <p><strong>UPDATE</strong>
  So If I have simple attack1, I need to call <code>enterFrameHandler()</code> in this function? And this will check if Hero have collision with Enemy? Or Where I need to use this function?</p>
  
  <pre><code>if (attack1)
  {
              enterFrameHandler();
      Hero.gotoAndStop("attack1");
  
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>This is how I declare enemy:</p>
  
  <pre><code>public var Enemy:Priesas = new Priesas; //Priesas is instance name of Enemy
  </code></pre>
  
  <p>Hero is choosing template by clicking buttons:</p>
  
  <pre><code>public function selectHero(what:int):void {
      // this is called with correct "what", design yourself. I use array index
      var whatHero:Class = heroes[what]; // get selected hero symbol
      if (Hero &amp;&amp; Hero.parent) Hero.parent.removeChild(Hero);
      // clean up previous hero. Drop listeners here, if any
      Hero = new whatHero(); // get new hero
      // process as usual, don't forget to "addChild(Hero)" somewhere
      create_hero();
  }
  
      function choosePlayer(event:MouseEvent):void {
          selectHero(0);
          start(event);
          }
  
       function create_hero()
       {
          addChild(Hero);
       }
  </code></pre>
  
  <p>So declared variables are: Hero and Enemy</p>
  
  <p><strong>UPDATE 3</strong></p>
  
  <p>For now I got following errors:</p>
  
  <pre><code>1120: Access of undefined property enemyClipBmpData.
  1180: Call to a possibly undefined method Point.
  1120: Access of undefined property heroClipBmpData.
  1180: Call to a possibly undefined method GlowFilter.
  </code></pre>
  
  <p>Here is how my code looks like now (I know about uppercase by creating vars, but for now I need to use like this)</p>
  
  <pre><code>    public function Script()
      {
  
          btn_play.addEventListener(MouseEvent.CLICK, start);
          btn_credits.addEventListener(MouseEvent.CLICK, choosePlayer);
          btn_control.addEventListener(MouseEvent.CLICK, start);
          stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
          stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
          stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
          addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true); //here added line
  
      }
  
      function enterFrameHandler(event:Event):void
      {
          Hero.x = mouseX;
          Hero.y = mouseY;
  
          if(enemyClipBmpData.hitTest(new Point(Enemy.x, Enemy.y),
                                  255,
                                  heroClipBmpData,
                                  new Point(Hero.x, Hero.y),
                                  255
  
                            ))
          {
              trace("hit");
              Enemy.filters = [new GlowFilter()];
          }
          else
          {
              Enemy.filters = [];
          }
      }
  
  function create_enemy()
          {
              addChild(Enemy);
              var enemyRect:Rectangle = Enemy.getBounds(this);
              var enemyClipBmpData = new BitmapData(enemyRect.width, enemyRect.height, true, 0);
              enemyClipBmpData.draw(Enemy);
              Enemy.x = 10;
              Enemy.y = 420;
          }
  
       function create_hero()
      {
  
          addChild(Hero);
          var heroRect:Rectangle = Hero.getBounds(this);
          var heroClipBmpData = new BitmapData(heroRect.width, heroRect.height, true, 0);
          heroClipBmpData.draw(Hero);
          Hero.gotoAndStop("stay");
          Hero.x = stage.stageWidth / 2;
  }
  </code></pre>