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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
julianopolitoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.