1 Reply Latest reply on Nov 3, 2007 12:37 AM by atta707

    How to create an Array of Button components in AS

    estacado1
      First I tried this:

      for (var j:uint = 0; j < 10; j++){
      var testButton:Button = new Button;
      testButton.label = "Test Button";
      testButton.addEventListener(MouseEvent.CLICK, function():void{trace("j = " + j);});
      addChild(testButton);
      }

      But every button's mouse click returns a trace of 10. I need the button to be aware of it's index. How do I go about doing this? I tried making an array:

      var testButton:Array = [];
      for (var j:uint = 0; j < 10; j++){
      testButton[j]:Button = new Button;
      testButton[j].label = "Test Button";
      testButton[j].addEventListener(MouseEvent.CLICK, function():void{trace("j = " + j);});
      addChild(testButton[j]);
      }

      But this gives a compile error:
      "Label must be simple identifier"
        • 1. Re: How to create an Array of Button components in AS
          atta707 Level 2
          You can use data property of the Button to store the index.

          You can use a ButtonBar to keep track of your buttons as a list.

          You can write a clickHandler as a normal function -- as opposed to anonymous one you have -- and pass the index of the button that you have stored in the data property.

          why you're not getting the behavior in the click handler that you're looking for? You'll need to study the scope and context for variables in Flex.

          Hope this helps.

          ATTA