开发者

How to create/naming var dynamicaly?

Hello for some reasons i need to create var dynamicaly.

Exemple

I have first an Array wich i want to use to "compose" my vars names

myArray:Array = new Array("aa","bb","cc");

In my final project this Array is created from a xml.

I want to do something like this, but doesn't work...

var ["myvar" + myArray[0]]:Sound = new Sound();
var ["myvar" + myArray[1]]:Sound = new Sound();
var ["myvar" + myArray[2]]:Sound = new Sound();

so it will be equal to write

var myvaraa:Sound = new Sound();
var myvarbb:Sound = new Sound();
var myvarcc:Sound = new S开发者_JS百科ound();

Anybody can guide me?


Two solutions :

1- Just create an object and attach properties:

var obj : Object = {};
obj.myvaraa = new Sound();
obj.myvarbb = new Sound();
obj["myvarcc"] = new Sound();

trace(obj.myvarcc);
trace(obj["myvarcc"]);

2- Make your class "dynamic" :

public dynamic class MyClass

And then you will abble to do :

this["myVarName"] = new Sound
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜