title: Actionscript 3 Score count on hitTestObject
body: <p>I've been working on this simple car game in Flash CS5. The car has to avoid the cars coming vertically and pick up coins. I have three types of coins which add 1, 2 and 3 score points on being picked up. My problem is that when I hit the coin with the car it goes through the car and gives much more points. I also have problem with removing it from the stage... Here the code so far:</p>
  
  <pre><code>var spex:Number = 0;
  var spey:Number = 4;
  var score:uint;
  var cars:Array = new Array  ;
  var db:Number = 2;
  var db_coins:Number = 1;
  var i:Number = 0;
  for (i=0; i&lt;=db; i++)
  {
      var traffic_mc:MovieClip = new traffic  ;
      cars.push(addChild(traffic_mc));
      cars[i].x = -500 * Math.random();
      cars[i].y = Math.random() * 400;
      trace(cars[i].y);
  }
  
  for (i=0; i&lt;=db_coins; i++)
  {
      var coin_y:MovieClip = new coin_yellow  ;
      coin_y.x = -500 * Math.random();
      coin_y.y = Math.random() * 400;
      addChild(coin_y);
      var coin_r:MovieClip = new coin_red  ;
      coin_y.x = -500 * Math.random();
      coin_y.y = Math.random() * 400;
      addChild(coin_r);
      var coin_b:MovieClip = new coin_blue  ;
      coin_b.x = -500 * Math.random();
      coin_b.y = Math.random() * 400;
      addChild(coin_b);
  }
  
  stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
  function keydown(k:KeyboardEvent):void
  {
      if (k.keyCode == 37)
      {
          spex -=  4;
  
      }
      if (k.keyCode == 39)
      {
          spex +=  4;
      }
  }
  stage.addEventListener(Event.ENTER_FRAME, go);
  function go(e:Event):void
  {
      this.auto.x +=  spex;
      if (this.auto.x &lt; 25)
      {
          this.auto.x = 25;
          spex = 0;
      }
      if (this.auto.x &gt; 286)
  {
      this.auto.x = 286;
      spex = 0;
      }
      for (i=0; i&lt;=db; i++)
      {
          if (cars[i].hitTestObject(this.auto))
          {
              trace("GAME OVER");
              stage.removeEventListener(KeyboardEvent.KEY_DOWN, keydown);
              stage.removeEventListener(Event.ENTER_FRAME, go);
              stage.addEventListener(KeyboardEvent.KEY_DOWN, retry);
          }
          cars[i].y +=  spey;
          if (cars[i].y &gt; 600)
          {
              cars[i].y = -50;
              cars[i].x = Math.random() * 251;
          }
      }
  
      for (i=0; i&lt;=db_coins; i++)
      {
          if (coin_y.hitTestObject(this.auto))
          {
              score +=  1;
              updateScore();
          }
          coin_y.y +=  spey-2;
          if (coin_y.y &gt; 600)
          {
              coin_y.y = -50;
              coin_y.x = Math.random() * 251;
          }
          if (coin_r.hitTestObject(this.auto))
          {
              score +=  2;
              updateScore();
          }
          coin_r.y +=  spey-2;
          if (coin_r.y &gt; 600)
          {
              coin_r.y = -50;
              coin_r.x = Math.random() * 251;
          }
          if (coin_b.hitTestObject(this.auto))
          {
              score +=  3;
              updateScore();
          }
          coin_b.y +=  spey-2;
          if (coin_b.y &gt; 600)
          {
              coin_b.y = -50;
              coin_b.x = Math.random() * 251;
          }
      }
  }
  
  stage.addEventListener(KeyboardEvent.KEY_DOWN, retry);
  function retry(k:KeyboardEvent):void
  {
      if (k.keyCode == 32)
      {
          stage.addEventListener(Event.ENTER_FRAME, go);
          stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
          for (i=0; i&lt;=db; i++)
          {
              cars[i].y = -300 * Math.random();
              cars[i].x = Math.random() * 251;
          }
  
          for (i=0; i&lt;=db_coins; i++)
          {
              coin_y.y = -300 * Math.random();
              coin_y.x = Math.random() * 251;
              coin_r.y = -300 * Math.random();
              coin_r.x = Math.random() * 251;
              coin_b.y = -300 * Math.random();
              coin_b.x = Math.random() * 251;
          }
          spex = 0;
          spey = 4;
          score = 0;
          scorecounter.text = "Score: " + score.toString();
      }
  }
  
  //Scorecount
  
  function init():void
  {
      score = 0;
      scorecounter.text = "Score: " + score.toString();
  }
  
  function updateScore():void
  {
      scorecounter.text = "Score: " + score.toString();
  }
  
  init();
  </code></pre>