Arrays AS3";对于每一个“;数组问题

Arrays AS3";对于每一个“;数组问题,arrays,actionscript-3,Arrays,Actionscript 3,我正在尝试创建一个显示您按下的按钮名称的系统。 按钮名称被放入一个数组中,但它只识别最后一个输入数组的项。 非常感谢您的帮助 var items:Array = [a, b, c]; //The name of each button for each(var index in items) { index.addEventListener(MouseEvent.CLICK, mouseClickHandler); } function mouseClickHandler(even

我正在尝试创建一个显示您按下的按钮名称的系统。 按钮名称被放入一个数组中,但它只识别最后一个输入数组的项。 非常感谢您的帮助

var items:Array = [a, b, c]; //The name of each button

for each(var index in items) 
{
    index.addEventListener(MouseEvent.CLICK, mouseClickHandler);
}

function mouseClickHandler(event:MouseEvent):void
{
    trace(index.name); //Should display the name of any of the buttons clicked.

}

这里只创建了一个
index
变量,而且
mouseClickHandler
函数显然只对其当前值起作用。如果需要引用特定值(在每个循环步骤中给出),则需要以某种方式对其进行本地化:

function generateClickHandler(index:someType) {
  return function(event:MouseEvent):void { trace(index.name); }
}

...    
for each(var index in items) 
{
    index.addEventListener(MouseEvent.CLICK, generateClickHandler(index);
}

我建议也检查一下。

这里只创建了一个
索引
变量,而且
mouseClickHandler
函数显然只对其当前值起作用。如果需要引用特定值(在每个循环步骤中给出),则需要以某种方式对其进行本地化:

function generateClickHandler(index:someType) {
  return function(event:MouseEvent):void { trace(index.name); }
}

...    
for each(var index in items) 
{
    index.addEventListener(MouseEvent.CLICK, generateClickHandler(index);
}

我建议您也检查一下。

您应该跟踪
currentTarget
名称:

var items:Array = [a, b, c]; //The name of each button

for each(var index in items) {
    index.addEventListener(MouseEvent.CLICK, mouseClickHandler);
}

function mouseClickHandler(event:MouseEvent):void {
    trace(event.currentTarget.name); //Should display the name of any of the buttons clicked.
}

您应该跟踪
currentTarget
名称:

var items:Array = [a, b, c]; //The name of each button

for each(var index in items) {
    index.addEventListener(MouseEvent.CLICK, mouseClickHandler);
}

function mouseClickHandler(event:MouseEvent):void {
    trace(event.currentTarget.name); //Should display the name of any of the buttons clicked.
}