Hmm , what are you trying to do ? There are a few questionable things happening. Your event-handler is named "handleButtonAdded" but it takes a ResizeEvent ? Do you mean to trigger an event based on when the button was added to the stage , or parent such as FlexEvent.ADDED or when the creation is complete , FlexEvent.CREATION_COMPLETE ?
The second thing is your "button" variable. It is declared outside of the loop in the "init" function. Meaning , when that loop is done and even during execution , it will point to the last button created. So all the buttons have listeners , meaning when one button fires off a ResizeEvent , it will remove the listener for whatever your variable "button" is pointing to. This guarantees (well , maybe not) the behaviour that your last button will NOT have a listener , therefore it will not fire.
By the way , you can set the label and the size of the button when you declare them. You don't have to subclass and use listeners.
This is extremely helpful-- thank you!
Let me ask a couple of follow-ups: What's the difference between FlexEvent.ADDED and FlexEvent.CREATION_COMPLETE and when should each be used?
Also, I see what you mean about declaring the button outside the loop, but how would I remove the event listener if I declare the button within the function (as it would be scoped locally)?
Lastly, on your point about declaring the label of a button locally, how would I do that if I want the number of the button to appear in its label (e.g. button 1, button 2, ... button 20)?
1 person found this helpful
1. One deals with adding a component to the displaylist , the other event deals with the lifecycle of a component.
You should have to do this , but if you do
public function eventHandler( event:Event):void
var button:Button = event.currentTarget as Button;
button.removeEventListener(SomeEvent.Event , eventHandler );
public function init():void
for(var i:int=0; i<numButtons;i++)
button.width = 100;
button.label = "Button " + numButtons.toString();
If this post answered your question , please mark it as such.