views:

65

answers:

1

I have a MXML button:

<mx:Button id="myButton1"/>

How do I create N number of Buttons with Actionscript: myButton2, myButton3, myButton4... myButtonN ?

And how to get the reference to the newly created buttons right after they are created? Like I should be able to do myButtonN.x = 100 right after it's created.

+2  A: 

This is pretty basic stuff...you might want to start with some Flex tutorials, or read through one of the many excellent books.

Here's a blob of code for you to copy & paste and see how it works for you:

private var buttons:Array = [];
public function createButtons():void {
  for(var i:int=0; i<100; i++) {
    buttons[i] = new Button();
    buttons[i].label = "Button "+i;
    buttons[i].x = i * 50;
    addChild(buttons[i]); // NOTE: use addElement instead of addChild in Flex 4
  }
}

It's not tested, so might need some minor typos, but you should be able to get the idea.

davr
+1, Also, did not know they changed it to addElement in Flex4. I wonder if joey lott is going to make another Programming Flex 4 book
John Isaacks
I did not think of using an array when asking the question. But oh well, this seems to work, so will use it. Thanks.
Yeti