flash as3动态实例变量名称/连接

时间:2011-02-18 15:55:46

标签: flash actionscript-3 variables dynamic instance

我有一系列项目类型代码,如下所示:

project_types_array[0] = "p"
project_types_array[1] = "exp"

和一组相应的影片剪辑,为actionscript导出,名称为:

type_p
type_exp

我想以某种方式根据数组中存在的项目类型在舞台上动态附加movieclip。我可以这样做:

for ( var i in project_types_array) {
    if (project_types_array[i] == "p"){
        var clip_p = new type_p();
        container.header.type_loader.addChild(clip_p);
    }
}

但我宁愿做这样的事情:

for(var i in project_types_array){

var "clip_" + project_types_array[i] = new "type_" + project_types_array[i]();
container.header.type_loader.addChild("clip_"+project_types_array[i]);

}

我如何实现这一目标?

1 个答案:

答案 0 :(得分:3)

尝试

var c:Class = getDefinitionByName('type_' + project_types_array[i]);
var spr:c = new c();

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getDefinitionByName%28%29