How do I add an event handler in a loop for ALL the buttons on Stage in Actionscript 3?

I Have a group of buttons, and I would like to add, looping through all instances, an event handler to them. I want to avoid entering a line for each one of them, so I need to loop through all and assign the event handler, and then I can use the event.target to differentiate the action for each one. How can I achieve this?
zyONAsked:
Who is Participating?
 
julianopolitoConnect With a Mentor Commented:
I think you are trying to mimic the for...in behavior of Actionscript 2.0 where you could do like :

for (var i in _root){
   _root[i].onPress= function(){};
}


In actionscript 3.0 it is not possible anymore this way, but you can use this:
for(var i:int = 0 ; i < root.numChildren; i++){
      var item:DisplayObject = root.getChildAt(i);//each item is a button, or anything visual on the document instance
      trace(item);
      item.addEventListener("click",onButtonClick);
}




0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
There are many ways to achieve this I think.

One way is to Extent the button component and in the constructor of that add the event listen and use this buttons in the group u wished to.

public function MyComponent()
        {
               
               addEventListener();
         }

The other way is to assign the parent container of this button group an ID and then later get all the children of that parent by using its ID ( those will be buttons type cast them if required) and then in a loop till the child count is reached for every child add the event handler accordingly.

I think there are still many other ways which I am not familiar with may be some other experts can help u better :)

Thank you.
 
0
 
zyONAuthor Commented:
Thank you, that's what I was looking for. Just some small corrections, the final valid AS3 could should be something like:

for(var i:int = 0; i < numChildren; i++){
      var item:DisplayObject = getChildAt(i);
      trace(item);
      item.addEventListener(MouseEvent.CLICK, listenerFunction);
}

I think getChildAt() and numChildren() are methods from the DisplayObjectContainer class.
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
julianopolitoCommented:
as you wrote :
"...loop for ALL the buttons on Stage..."

So I assumed the code should be put in the constructor of a sample class in flex, or in timeline for a sample flash document. "numChildren" here  is in the scope of the timeline or document class constructor, this is equivalent to this.numChildren, and "this" is the document class instance, which in turn is always an instance of Sprite or MovieClip, both DisplayObjectContainer subclasses. The same for getChildAt (as the scope of my example is the same o numChildren).

I'm sorry if I did not explicitly said what the scope was, but I thought you'd understand that.

Also numChildren is a property, not a method (not explicitly)
0
 
zyONAuthor Commented:
julianopolito: You're right. Thank you for the explanation. And yeah, numChildren is not a method but a property.
0
 
julianopolitoCommented:
no problem, but my grade could be A teacher! ;)
0
 
zyONAuthor Commented:
Guess it's not possible to change it. Sorry.
0
 
julianopolitoCommented:
no problem, just kidding man. ;)
0
All Courses

From novice to tech pro — start learning today.