user3213109
user3213109

Reputation: 24

Creating variable names

I'm having trouble with the following function:

private function whichLevelToLoad():void{
    if(levelToLoad == "nothing"){
        currentLevel = null;
    }

    var thisObj:Object = new Object();

    if(levelBtnArray!=null){
        for(var j:int=levelBtnArray.length-1;j>=0;j--) {
            if(levelToLoad == String("level " + (j+1))){
                thisObj["level"+(j+1)] = new ["Level"+(j+1)]();--------------------------->The Problem
                thisObj["level" + (j+1)].x = 0;
                thisObj["level" + (j+1)].y = 0;
                addChildAt(thisObj["level" + (j+1)], 0);
                currentLevel = thisObj["level" + (j+1)];
            }
        }
    }
}

I'm trying to instatiate 75 objects by using a loop. The line would look like this,"thisObj.level1 = new Level1(); with the numbers going from 1-75. Is this possible? How do I do it?

Upvotes: 0

Views: 43

Answers (1)

3vilguy
3vilguy

Reputation: 981

Try

if(levelBtnArray!=null){
    var levelClass:Class;

    for(var j:int=levelBtnArray.length-1;j>=0;j--) {
        if(levelToLoad == String("level " + (j+1))){

            levelClass = getDefinitionByName( "Level"+(j+1) ) as Class;
            thisObj["level"+(j+1)] = new levelClass();

            thisObj["level" + (j+1)].x = 0;
            thisObj["level" + (j+1)].y = 0;
            addChildAt(thisObj["level" + (j+1)], 0);
            currentLevel = thisObj["level" + (j+1)];
        }
    }
}

Upvotes: 2

Related Questions