动作 - 添加事件监听在舞台上的多个按钮 [英] Actionscript - Adding EventListener to multiple buttons on stage

查看:188
本文介绍了动作 - 添加事件监听在舞台上的多个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,增加事件监听多个对象在舞台上。我上面有在舞台上名为Button01,Button02..Button4040个按键,和我正在寻找最简单的添加事件监听到所有这些方法。

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.

创建像

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 :(.

在此先感谢。

推荐答案

您可以做这样的事情:

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);
}

这假定该code是在的DocumentClass,或你的时间表上的某个地方,因为它是使用访问影片剪辑。如果不是的话,那么只需更换与容器的参考。

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.

这篇关于动作 - 添加事件监听在舞台上的多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆