1

Am trying to create new instances of classes in a loop where the class instance names have been defined in an array. how Do i turn the array values from string to objects? here's what i have written so far

var tiles2:Array = new Array("e1", "e2", "e3", ...);

for (var i = 0; i < tiles2.length; i++){
    if (i == 0){
        xPos = 18;
    }else if (xPos > 0 && xPos < maxGridWidth){
        xPos =+ xPos + objWidth + horGap;
        //trace(xPos);
    }else{
        xPos = 18;
        yPos =+ yPos + verGap + objHeight;
    }
    var this[tiles2[i]] = new tiles2[i];
    this[tiles2[i]].name = tiles2[i];
    this[tiles2[i]].x = xPos;
    this[tiles2[i]].y = yPos;
    tileArray[i] = this[tiles2[i]];
    addChild(this[tiles2[i]]);
}

This is where I have a problem var mc = new tiles2[i];. the desired output am looking is something like

var e1 = new e1;
e1.name = tiles2[i];
e1.x = xPos;
e1.y = yPos;
tileArray[i] = e1;
addChild(e1);

if you have a better procedure for doing this i will be glad if you can show me

1 Answer 1

1

I believe you are looking for this...

// If tiles2[i] is a string that is the name of a class
var type:Class = getDefinitionByName(tiles2[i]) as Class;
var thing = new type();

// If tiles2[i] is a string instance name for an existing object:
var thing:DisplayObject = getChildByName(tiles[i]);

OR, perhaps you just need to change the line in question to this:

//var mc = new tiles2[i]; <-- line in question

//take out the var, and instantiate the proper class
this[tiles2[i]] = new e1();
Sign up to request clarification or add additional context in comments.

7 Comments

tiles2 is just an array I created that holds names of objects created on stage that are in my asset library. it is not the name of a class file
@NelsonSule - it's not clear what anything is in your code - this answer looks to me to explain what you're trying to do - Since it apparently isn't, I took a guess and added it to this answer.
Or he even might be meaning to var tileClassname:String = getQualifiedClassName(this[tiles2[i]]); var tileClass:Class = getDefinitionByName(tileClassname) as Class; var newTile = new tileClass();
@kaarto Thanks for responding. I have created assets on stage with linkage class names such as e1, e2, ... instead of writing a code that will create instances of each one by one, I am looking for a way easily automate this tedious process. So I created an array with the names of the assets tiles2=["e1", "e2",...]. Now, what i want to achieve is turning each string in the array to a variable name and a class identifier e.g var e1 = new e1(); for all the values of the array
So those are linkage names? Then Andrew's first solution should work for you, did you test it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.