开发者

flash as3 dynamic instance variable name / concatenation

开发者 https://www.devze.com 2023-02-11 03:13 出处:网络
I have an array of project type codes like this: project_types_array[0] = \"p\" project_types_array[1] = \"exp\"

I have an array of project type codes like this:

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

etc.

and a corresponding set of movie clips, exported for actionscript, with n开发者_JAVA百科ames:

type_p
type_exp

etc.

I want to somehow dynamically attach a movieclip on the stage according to the project type that exists in the array. I could just do something like this:

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);
    }
}

but I'd rather do something like this:

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]);

}

How do I achieve this?


try

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

0

精彩评论

暂无评论...
验证码 换一张
取 消