使用带有事件处理程序的Movie剪辑for循环

时间:2013-09-28 12:03:37

标签: flash actionscript-3

我的按钮是实体名称为字母的影片剪辑。我想用循环来获取值。这是代码

var buttons:Array;
  buttons = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I', 'J', 'K', 'L', 
          'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

function gettracedValue {

for(var i= 0; i < buttons.length; i++ ) {

    var btnName:String = buttons[i];

       trace("Buttons " + btnName);

      btnName.addEventListener(MouseEvent.MOUSE_UP, traceValue);

      } //end for

 }//end gettracedValue 

 function traceVaalue():void{
    trace("Clicked: " + event.target.name);

 }

当我运行代码时,我得到了

TypeError: Error #1010: A term is undefined and has no properties.
at hangMan_fla::MainTimeline/gettracedValue()

1 个答案:

答案 0 :(得分:2)

您无法将事件侦听器分配给字符串。

var buttons:Array = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
function gettracedValue() {
    for(var i:uint= 0; i < buttons.length;  i++) {
        var btn:MovieClip = getChildByName( buttons[i] ) as MovieClip;
        btn.addEventListener(MouseEvent.CLICK, traceValue);
    }
}
function traceValue(e:MouseEvent):void{
    trace("Clicked: " + e.target.name);
}