【问题标题】:addEventListener(object[i]) in for loop gives me one object not multiplefor 循环中的 addEventListener(object[i]) 给了我一个对象而不是多个
【发布时间】:2013-03-16 14:47:38
【问题描述】:

我正在为游戏制作排行榜。此排行榜从数组中获取分数。但是当我添加 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}]

代码:

public function toonHighscoreArray():Array {
highScoreTabel.sortOn(["score"], [Array.NUMERIC]).reverse();//get highest score on top
var returnArray:Array = new Array();
for ( var i:int = 0; i < 6; i++ ) {
    var scores:TextField = new TextField();
    scores.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){toon2deSpeler(highScoreTabel[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);
}
return returnArray;
}

private function toon2deSpeler(score:Object) {
    trace(score.naam);
}

我希望函数 toon2deSpeler 在单击 wilbert 在文本字段中的文本字段时跟踪 wilbert,并在单击 harry 的文本字段时跟踪 harry

但是当我点击 wilbert 以及点击 harry 或 joost 等时它给了我 joost。

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

【问题讨论】:

    标签: arrays actionscript-3 object textfield addeventlistener


    【解决方案1】:

    循环内的闭包不会按预期工作,一旦调用事件处理程序,它将使用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]);
       });
    }
    

    【讨论】:

    • 这给了我文本字段,但我想要对象
    • 更新了我的答案,也请参阅@David 的答案,您应该真正扩展 TextField 并创建自己的类并将存储在这些对象中的属性放入该类中。
    【解决方案2】:

    函数在创建它们的范围内运行 (See this page on Function scope),因此您的内联侦听器:

    function(e:MouseEvent){toon2deSpeler(highScoreTabel[i])}
    

    使用来自toonHighscoreArray()i,而不是i 的“自己”副本。不过,鉴于您的代码,您将获得一个空对象引用而不是“joost”,因为您正在尝试访问 highScoreTabel[6]。

    我真的建议扩展 TextField 并使用 highScoreTabel 的属性创建对象,然后使用 Barış Uşaklı 的方法。但是,可以像这样在自己的范围内创建每个侦听器函数:

    function getScoreClickListener(scoreID:uint):Function {
        return function(e:MouseEvent){toon2deSpeler(highScoreTabel[scoreID])}
    }
    

    然后在添加你的监听器时使用它:

    scores.addEventListener(MouseEvent.CLICK, getScoreClickListener(i));
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2021-09-24
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      相关资源
      最近更新 更多