2014-01-19 191 views
0
我在用下面的函數麻煩

創建變量名

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

我試圖通過使用循環instatiate 75級的對象。該生產線是這樣的,「thisObj.level1 =新1級();?用數字1-75打算這可能我這樣做,如何做

回答

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