title: flex button event listeners in Class
body: <p>I have many buttons in Main.mxml. I'm trying to move the button functionality into a Class and have Event Listeners inside the class respond to Click and call other functions. I have written:</p>
  
  <p><strong>Main.mxml</strong></p>
  
  <pre><code>&lt;mx:Button x="23.5" y="10" label="checker" click="{goListen()}" /&gt;
  &lt;mx:Button id="btnT1" x="252.5" y="10" label="t1" /&gt;
  &lt;mx:Button id="btnT2" x="309" y="10" label="t2"/&gt;
  &lt;mx:Button id="btnT3" x="366" y="10" label="t3"/&gt;
  </code></pre>
  
  <p><strong>Main.as</strong></p>
  
  <pre><code>private function goListen():void
      {
          var t:ButtonListener = new ButtonListener(btnT1, btnT2, btnT3);
      }
  </code></pre>
  
  <p><strong>ButtonListener.mxml</strong></p>
  
  <pre><code>package com.util
  </code></pre>
  
  <p>{
      import flash.events.EventDispatcher;
      import flash.events.MouseEvent;</p>
  
  <pre><code>import mx.controls.Alert;
  import mx.controls.Button;
  
  public final class ButtonListener extends EventDispatcher
  {
      private var __btnArray:Array;
  
      public function ButtonListener(...btnList)
      {
          __btnArray = new Array();
  
          for each (var item:Button in btnList)
          {
              __btnArray.push(item);
          }
  
          buildListeners();
      }
  
      private function buildListeners():void
      {
          for each (var item:Button in __btnArray)
          {
              item.addEventListener(MouseEvent.CLICK, traceMe, false, 0, true);
          }
      }
  
      private function traceMe(event:MouseEvent):void
      {
          trace(event.target.label + " was clicked");
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>so when I debug, I see the array filling up with the buttons, but the traceMe() function won't work. Not sure how I can get this to work. Or do I just have to have 30 event listeners and corresponding functions in the main class. </p>