Actionscript - Adding EventListener to multiple buttons on stage

一个人想着一个人 提交于 2019-12-12 02:26:43

问题


I have a little problem with adding EventListener to multiple objects on stage. I have above 40 buttons on stage named "Button01","Button02" .. "Button40", and i'm looking for easiest way to add EventListener to all of them.

Creating something like

Button01.addEventListener(MouseEvent.CLICK, doSomething)
Button02.addEventListener(MouseEvent.CLICK, doSomething)
..
Button40.addEventListener(MouseEvent.Click, doSomething)

(Notice the same function). isn't solution i'm looking for :(.

Thanks in advance.


回答1:


You could do something like this:

var cnt:Number;
var cnt_str:String;
for (cnt = 1; cnt <= 40; cnt++) {
    if (cnt < 10) {
        cnt_str = "0" + String(cnt);
    } else {
        cnt_str = String(cnt);
    }
    this["Button" + cnt_str].addEventListener(MouseEvent.CLICK, doSomething);
}

This assumes that this code is in the DocumentClass, or on your timeline somewhere since it is using this to access the MovieClips. If that is not the case then just replace the this with a reference of the container.



来源:https://stackoverflow.com/questions/7134618/actionscript-adding-eventlistener-to-multiple-buttons-on-stage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!