1

私が持っている:

stage.getChildByName("button_1")

button_1

button_2

button_3

button_...

button_15

var XSampleを簡単に作成して、数値を格納し、ステートメントを実行し、+ 1 XSampleを増やし、「button_」でXSample.toString()を使用できます。

しかし、物事はもう少し複雑になるので、button_の後にすべてを取得する必要があります

stage.getChildByName("button_" + everything)
// it could be button_5, button_Ag6, button_0086, button_93 and so on

正規表現でそれを行うにはどうすればよいですか?

ありがとう

4

1 に答える 1

2

使用事例:

import flash.display.Shape;
import flash.display.Sprite;

var shapeContainer:Sprite = new Sprite();

addChild(shapeContainer);

var shape_1:Shape = new Shape();
shape_1.name = "shape_ONE";

var shape_2:Shape = new Shape();
shape_2.name = "displayObject_TWO";

var shape_3:Shape = new Shape();
shape_3.name = "shape_THREE";

shapeContainer.addChild(shape_1);
shapeContainer.addChild(shape_2);
shapeContainer.addChild(shape_3);

trace(getIndicesWithChildNamePattern("shape_", shapeContainer)); //0, 2

String.indexOf() の使用:

function getIndicesWithChildNamePattern(pattern:String, container:DisplayObjectContainer):Vector.<uint>
{
    var indices:Vector.<uint> = new Vector.<uint>();

    for (var i:uint = 0; i < container.numChildren; i++)
    {
        if (container.getChildAt(i).name.indexOf(pattern) != -1)
        {
            indices.push(i);
        }
    }

    return indices;
}

正規表現の使用:

function getIndicesWithChildNamePattern(pattern:String, container:DisplayObjectContainer):Vector.<uint>
{
    var indices:Vector.<uint> = new Vector.<uint>();
    var regExp:RegExp = new RegExp(pattern, "g");

    for (var i:uint = 0; i < container.numChildren; i++)
    {
        if (container.getChildAt(i).name.match(regExp).length)
        {
            indices.push(i);
        }
    }

    return indices;
}
于 2013-02-02T02:47:33.633 に答える