0

次の例に従って、連結を使用してこの関数内の変数にアクセスする必要があります。

public function movePlates():void
{
    var plate1:Plate;
    var plate2:Plate;
    var cont:uint = 0;

    for (var i:uint = 0; i < LAYER_PLATES.numChildren; i++)
    {
        var tempPlate:Plate = LAYER_PLATES.getChildAt(i) as Plate;

        if (tempPlate.selected)
        {
            cont ++;

            this["plate" + cont] = LAYER_PLATES.getChildAt(i) as Plate;
        }
    }
}

編集:

public function testFunction():void
{
    var test1:Sprite = new Sprite();
    var test2:Sprite = new Sprite();
    var tempNumber:Number;
    this.addChild(test1);
    test1.x = 100;
    this.addChild(test2);
    test2.x = 200;

    for (var i:uint = 1; i <= 2; i++)
    {
        tempNumber += this["test" + i].x;
    }

    trace("tempNumber: " + tempNumber);
}

このようなコードを実行すると、行 this["test" + i] はクラスの変数を返します。関数の変数であるローカル変数が必要です。

4

2 に答える 2

0

[] 表記ではローカル変数を取得できません。あなたのケースには多くの解決策があります。辞書または getChildAt() 関数を使用できます。

function testFunction():void
{
    var dict = new Dictionary(true);
    var test1:Sprite = new Sprite();
    var test2:Sprite = new Sprite();
    var tempNumber:Number = 0;

    addChild(test1);
    dict[test1] = test1.x = 100;

    addChild(test2);
    dict[test2] = test2.x = 200;

    for (var s:* in dict)
    {
        tempNumber += s.x;
        //or tempNumber += dict[s];
    }

    trace("tempNumber: " + tempNumber);
};
于 2013-04-02T08:21:19.550 に答える