用例:
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;
}