title: How to give dynamically created buttons actions for each one - part 2
body: <p>Hi again fellow Flashers :)</p>
  
  <p>My first question poised here at StackOverFlow dealt with this issue, I had an array which created a few different buttons. However I didn't know how to assign actions to them:
  <a href="https://stackoverflow.com/questions/1381178/how-to-give-dynamically-created-buttons-actions-for-each-one"><strong>How to give dynamically created buttons actions for each one - part 1</strong></a></p>
  
  <p>Thanks to <a href="https://stackoverflow.com/users/87002/joel-hooks">Joel Hooks</a>, I was able to get my code working. However this time around instead of using an imported graphic as a Class from my library, I'm instead creating a movieClip by drawing a gradient*(didn't need to import a jpg)*. Now I'm getting another dreaded <strong>"not compile time constant"</strong> error again. I believe it has something to do with how I'm trying to determine the button instance that is clicked or rolledOver.</p>
  
  <p><strong>The Previous button creation <em>(working)</em> code:</strong></p>
  
  <p>for (var i:Number=0; i &lt; myXMLArray.length; i++) {
    navButton=new NavButton  ;
    navButton.name="button" + i;
    navButton.x=i * navSIZE;
    navButton.y=navBtnY;
    navButton.buttonMode=true;
    thumbsMov.addChild(navButton);</p>
  
  <p>buttons.push(navButton);
    buttons[i].addEventListener(MouseEvent.MOUSE_UP,handleButtonClick);
    buttons[i].addEventListener(MouseEvent.ROLL_OVER,handleButtonOver);
    buttons[i].addEventListener(MouseEvent.ROLL_OUT,handleButtonOff);
   }</p>
  
  <p><strong>The Previous listener <em>(working)</em> code:</strong></p>
  
  <pre><code>function handleButtonOver(event:MouseEvent):void {
  </code></pre>
  
  <p>var button:NavButton = event.target as NavButton;
    var id:Number = Number(button.name.split("button")<a href="https://stackoverflow.com/questions/1381178/how-to-give-dynamically-created-buttons-actions-for-each-one">1</a>);
    if(button)
     TweenLite.to(buttonRolls[id], .4, {alpha:1, ease:Strong.easeOut});
     cataText[id].defaultTextFormat = a12Green; 
     cataText[id].text = myXMLArray[id].id;
   }</p>
  
  <p><strong>Current button creation code:</strong></p>
  
  <pre><code>for (var i:Number = 0; i &lt; num; i++) {
  trace("loop number = "+i);
  
  var blueGradient:MovieClip = new MovieClip();
   blueGradient.name = "button" +i;
   blueGradient.graphics.beginGradientFill(bGrad, bColors, bAlphas, bRatios ,bMatrix)
   blueGradient.graphics.drawRect(0, 0, 266, 40);
   blueGradient.x = i * navSIZE;
   blueGradient.y = navBtnY;
   blueGradient.buttonMode = true;
  addChild(blueGradient);
  
  buttons.push(blueGradient)
  
  buttons[i].addEventListener(MouseEvent.MOUSE_UP, handleButtonClick);
  buttons[i].addEventListener(MouseEvent.ROLL_OVER, handleButtonOver);
  buttons[i].addEventListener(MouseEvent.ROLL_OUT, handleButtonOff);
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Current button listener code:</strong></p>
  
  <pre><code>function handleButtonOver(event:MouseEvent):void {
  var button:blueGradient = event.target as blueGradient;
  var id:Number = Number(blueGradient.name.split("button")[1]);
  if(blueGradient)
   cataText[id].defaultTextFormat = navFontRoll;
   cataText[id].text = myArray[id].id;
  </code></pre>
  
  <p>}</p>
  
  <p>The current movie will only run without an error if the following line is commented out:</p>
  
  <pre><code>var button:blueGradient = event.target as blueGradient;
  </code></pre>
  
  <p>Because that is what is causing the not compile time constant error to come up. <strong>However by removing that line, all buttons are button1 instead of 2 buttons dynamically created named button0 and button1.</strong></p>
  
  <p>Any help or pointers here would be greatly appreciated! Thanks in advance for taking a look at my code.</p>