1

我有按钮,它是带有字母实例名称的影片剪辑。我想使用 for 循环来获取价值。这是代码

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()
4

1 回答 1

2

您不能将 eventListener 分配给字符串。

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);
}
于 2013-09-28T12:58:55.693 回答