Arrays for循环中的addEventListener(object[i])给了我一个对象,而不是多个对象

Arrays for循环中的addEventListener(object[i])给了我一个对象,而不是多个对象,arrays,actionscript-3,object,textfield,addeventlistener,Arrays,Actionscript 3,Object,Textfield,Addeventlistener,我正在制作一个游戏排行榜。此排行榜从A数组中获取分数。但是当我添加eventListener时,我从数组中只得到一个对象。 这是我的对象数组: [{gamenr:1,naam:"wilbert", score:60},{gamenr:1,naam:"joost", score:20}, {gamenr:2,naam:"harry", score:50},{gamenr:2,naam:"john", score:10}, {gamenr:3,naam:"carl", score:30},{game

我正在制作一个游戏排行榜。此排行榜从A数组中获取分数。但是当我添加eventListener时,我从数组中只得到一个对象。 这是我的对象数组:

[{gamenr:1,naam:"wilbert", score:60},{gamenr:1,naam:"joost", score:20},
{gamenr:2,naam:"harry", score:50},{gamenr:2,naam:"john", score:10},
{gamenr:3,naam:"carl", score:30},{gamenr:3,naam:"dj", score:16}]
代码:

公共函数toonHighscoreArray():数组{
highScoreTabel.sortOn([“score”],[Array.NUMERIC]).reverse();//在顶部获得最高分数
var returnArray:Array=new Array();
对于(变量i:int=0;i<6;i++){
变量得分:TextField=newtextfield();
scores.addEventListener(MouseEvent.CLICK,函数(e:MouseEvent){toon2deSpeler(highscorestabel[i]);
得分y=(i*50)-50;
得分:身高=50;
scores.text=”“+(i+1)+“\t”+高分表[i]。naam+“met”+高分表[i]。分数+“punten。”;
scores.autoSize=“left”;
returnArray.push(分数);
}
返回数组;
}
专用函数toon2deSpeler(分数:对象){
trace(score.naam);
}
我希望函数toon2deSpeler在单击文本字段时跟踪wilbert,单击wilbert所在的文本字段时跟踪wilbert,单击harry的文本字段时跟踪harry

但是当我点击威尔伯特的时候,它给了我joost,但当我点击harry或joost等的时候,它也给了我joost


如何在toon2deSpeler中获得正确的对象作为参数?

循环内的闭包不会像您预期的那样工作,一旦调用事件处理程序,它将使用
i
的最后一个值

将for循环更改为:

for ( var i:int = 0; i < 6; i++ ) {
    var scores:TextField = new TextField();
    addScoreListener(scores, i);

    scores.y = (i * 50) - 50;
    scores.height = 50;
    scores.text = "" + (i + 1) + ".\t" + highScoreTabel[i].naam + " met " + highScoreTabel[i].score + " punten.";
    scores.autoSize = "left";

    returnArray.push(scores);
}

private function addScoreListener(score:TextField, index:int):void
{
   scores.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{
       toon2deSpeler(highScoreTabel[index]);
   });
}
for(变量i:int=0;i<6;i++){
变量得分:TextField=newtextfield();
addScoreListener(分数,i);
得分y=(i*50)-50;
得分:身高=50;
scores.text=”“+(i+1)+“\t”+高分表[i]。naam+“met”+高分表[i]。分数+“punten。”;
scores.autoSize=“left”;
returnArray.push(分数);
}
私有函数addScoreListener(分数:TextField,索引:int):void
{
scores.addEventListener(MouseEvent.CLICK),函数(e:MouseEvent):void{
toon2deSpeler(高分表[指数]);
});
}

循环内的闭包不会像您预期的那样工作,一旦调用事件处理程序,它将使用
i
的最后一个值

将for循环更改为:

for ( var i:int = 0; i < 6; i++ ) {
    var scores:TextField = new TextField();
    addScoreListener(scores, i);

    scores.y = (i * 50) - 50;
    scores.height = 50;
    scores.text = "" + (i + 1) + ".\t" + highScoreTabel[i].naam + " met " + highScoreTabel[i].score + " punten.";
    scores.autoSize = "left";

    returnArray.push(scores);
}

private function addScoreListener(score:TextField, index:int):void
{
   scores.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{
       toon2deSpeler(highScoreTabel[index]);
   });
}
for(变量i:int=0;i<6;i++){
变量得分:TextField=newtextfield();
addScoreListener(分数,i);
得分y=(i*50)-50;
得分:身高=50;
scores.text=”“+(i+1)+“\t”+高分表[i]。naam+“met”+高分表[i]。分数+“punten。”;
scores.autoSize=“left”;
returnArray.push(分数);
}
私有函数addScoreListener(分数:TextField,索引:int):void
{
scores.addEventListener(MouseEvent.CLICK),函数(e:MouseEvent):void{
toon2deSpeler(高分表[指数]);
});
}

函数在创建它们的作用域()中运行,因此您的内联侦听器:

function(e:MouseEvent){toon2deSpeler(highScoreTabel[i])}
scores.addEventListener(MouseEvent.CLICK, getScoreClickListener(i));
正在使用
toonHighscoreArray()
中的
i
,而不是
i
的“自有”副本。但是,如果代码在那里,当您试图访问highScoreTabel[6]时,您将得到一个null对象引用,而不是“joost”

我真的建议扩展
TextField
并使用
HighScoreTable
中的属性创建对象,然后使用BarıUşaklı的方法。但是,可以在自己的作用域中创建每个侦听器函数,如下所示:

function getScoreClickListener(scoreID:uint):Function {
    return function(e:MouseEvent){toon2deSpeler(highScoreTabel[scoreID])}
}
然后在添加偶数侦听器时使用该选项:

function(e:MouseEvent){toon2deSpeler(highScoreTabel[i])}
scores.addEventListener(MouseEvent.CLICK, getScoreClickListener(i));

这使得以后很难删除事件侦听器,因此您需要单独跟踪它们。

函数在创建它们的作用域()中运行,因此您的内联侦听器:

function(e:MouseEvent){toon2deSpeler(highScoreTabel[i])}
scores.addEventListener(MouseEvent.CLICK, getScoreClickListener(i));
正在使用
toonHighscoreArray()
中的
i
,而不是
i
的“自有”副本。但是,如果代码在那里,当您试图访问highScoreTabel[6]时,您将得到一个null对象引用,而不是“joost”

我真的建议扩展
TextField
并使用
HighScoreTable
中的属性创建对象,然后使用BarıUşaklı的方法。但是,可以在自己的作用域中创建每个侦听器函数,如下所示:

function getScoreClickListener(scoreID:uint):Function {
    return function(e:MouseEvent){toon2deSpeler(highScoreTabel[scoreID])}
}
然后在添加偶数侦听器时使用该选项:

function(e:MouseEvent){toon2deSpeler(highScoreTabel[i])}
scores.addEventListener(MouseEvent.CLICK, getScoreClickListener(i));

这使得以后很难删除事件侦听器,因此您需要单独跟踪它们。

这给了我文本字段,但我希望对象更新我的答案,请参阅@David's answer,你应该扩展TextField,创建自己的类,并将存储在这些对象中的属性放入该类。这给了我TextField,但我希望对象更新我的答案,请参见@David的答案,你应该真正扩展TextField,创建自己的类,并将存储在这些对象中的属性放入该类。