title: AS3 .addEventListener in a loop?
body: <p>I am creating a line of <code>Sprite</code> elements. Every sprite element has different job, when it is clicked. How can I make the function inside the <code>addEventListener</code> to know which button was clicked?</p>
  
  <p>In this case, the <code>traced</code> value of <code>i</code> when it is cliicked is <code>always 6</code>. Which is wrong, because 6 is only the last element of the array. What about the rest of them, the beginning?</p>
  
  <pre><code>for (var i:int = 0; i &lt; 6; i++) {
      var barPart:Sprite = new Sprite();
      barPart.x = i * (30);
      barPart.y = 0;
      barPart.graphics.beginFill(0x000000, 0.2);
      barPart.graphics.drawRect(0, 0, 10, 10);
      barPart.graphics.endFill();
  
      barPart.addEventListener(MouseEvent.CLICK, function(_event:MouseEvent):void {
           trace(i);
      });
  }
  </code></pre>