创建变量名称

时间:2014-01-19 22:35:55

标签: actionscript-3

我在使用以下功能时遇到问题:

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

我正在尝试使用循环来实现75个对象。该行看起来像这样,“thisObj.level1 = new Level1();数字从1-75开始。这可能吗?如何? 我会这样做吗?

1 个答案:

答案 0 :(得分:2)

尝试

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