2013-05-26 57 views

回答

1

不,不是我見過或聽說過yet.The循環需要一個方法定義內運行,你不能宣佈public static var的方法中。變量名也不能是動態的,編譯器應該在編譯時檢查語法錯誤,以及它如何知道你是否在運行時命名變量?不允許使用局部變量的動態名稱。

出於您的目的,您可以使用Array,DictionaryObject通過循環存儲多個值。但是,您不能在循環內使用public static

var myVarMap:Object = {}; 
for(var i:uint=0;i<22;i++) 
{ 
    myVarMap['Name' +i] = Texture(....); 
} 
0

雖然,我認爲新白癡的答案使用靜態數組應該爲您的目的,這將保持邏輯更具可讀性。但是,要回答您的問題,如果我們可以動態地在類上創建公共靜態屬性:是的,您可以通過使用原型對象。見下面的代碼。

package 
{ 
    import flash.display.Sprite; 

    public class dingAS3 extends Sprite 
    { 
     public function dingAS3() 
     { 
      DynClass.init(); 

      var a:DynClass = new DynClass; 
      for(var i:String in a) 
      { 
       trace(i + '-->' + a[i]); 
      } 

      var b:DynClass = new DynClass; 

      DynClass.prototype["prop0"] = "Hello"; 

      trace('b: ' + b["prop0"]); 
     } 
    } 
} 


class DynClass 
{ 
    public static function init():void 
    { 
     for(var i:uint=0; i<5; i++) 
     { 
      DynClass.prototype["prop"+i] = i; 
     } 
    } 
} 

上面代碼的輸出會是這樣的:

prop1-->1 
prop3-->3 
prop2-->2 
prop0-->0 
prop4-->4 
b: Hello 
0

通常情況下,沒有爲新的白癡指出, 但有辦法,可以幫助。

實際上,您可以使用執行ABC(動作腳本代碼)操作的庫(如as3swf,as3-commons-bytecode)生成一些代碼,但這對於您的方案來說很複雜。 catholicon的解決方案要簡單得多,但在使用動態類時要小心也是一個好主意。

我假設你有一個資產的文件夾,你想根據它生成一個資產類。它應該只是一個使用任何您熟悉的語言,可以掃描您的文件系統的問題:cmd/bash/c/C++/python/java甚至是JavaScript,儘管Flash IDE中的JSFL或ActionScript作爲最小的AIR工具來爲你生成這個資產類。

下面是使用Python一個簡單的例子:

import sys,os,re 

classTemplate = "package{\n\n\tpublic class %s{\n%s\n\t}\n}" #template for empty class with variables placeholder 
varTemplate = "\t\tpublic static var %s = new Texture('%s');\n" #variable template with 2 placeholders: variable name and file path 
vars = "" #all vars to be placed in the class variables place holder 

clean = lambda varStr: re.sub('\W|^(?=\d)','_', varStr) #cleanup the filename so it's a valid actionscript variable name 

if(len(sys.argv) == 4): #if we have all the arguments 
    f = sys.argv[1] 
    if(os.path.exists(f) == False): #check if the path exists 
     print f+' does not exist' 
     pass 

    if(f != None and os.path.isdir(f)): # also if it's a directory 
     files = [] 
     for (dirpath, dirname, filenames) in os.walk(f): #travers the path and remember files 
      files.extend(filenames) 
      break 
     for tf in files: # for each file 
      var = clean(tf) # create a variable 
      vars += varTemplate % (var,dirpath+os.sep+tf) # replace the variable placeholders/tokens and append to all class vars 
     asClass = open(sys.argv[3],'w') #finally open the output file 
     asClass.write(classTemplate % (sys.argv[2],vars)) # replace and write the class 
     asClass.close() # and close the file 
    else: 
     print f +' is not a directory' 

如果您保存此作爲assetgen.py例如,你可以像這樣運行:

python /path/to/assetgen.py /path/to/your/assets_dir AS3ClassName /path/to/write/AS3ClassName.as 

所以我的機器上的一個簡短的測試會是什麼樣子:

python ~/Desktop/assetgen.py ~/Documents/assets/240x240_240x230_pxl/240x240/1-1 Assets1_1 ~/Desktop/Assets1_1.as 

其中產生:

package{ 

    public class Assets1_1{ 
     public static var _01_bmp = new Texture('/Users/hm/Downloads/240x240_240x230_pxl/240x240/1-1/01.bmp'); 
     public static var _02_bmp = new Texture('/Users/hm/Downloads/240x240_240x230_pxl/240x240/1-1/02.bmp'); 
     public static var _03_bmp = new Texture('/Users/hm/Downloads/240x240_240x230_pxl/240x240/1-1/03.bmp'); 
     public static var _04_bmp = new Texture('/Users/hm/Downloads/240x240_240x230_pxl/240x240/1-1/04.bmp'); 
     public static var _05_bmp = new Texture('/Users/hm/Downloads/240x240_240x230_pxl/240x240/1-1/05.bmp'); 

    } 
} 

如果你正在使用Ant例如(可集成到像Eclipse/FlashBuilder/FDT IDE),您可以輕鬆地生成一個任務來重新生成類作爲資產項目中的更改/更新。

隨意隨意生成變量名稱,添加額外的命令行參數以指定actionscript包等。

如上所述,上述腳本(遍歷資產目錄並替換字符串以生成動作類文件)可以使用您熟悉的語言編寫。