루프에서 public static var을 생성 할 수 있습니까? 같은 뭔가 : 내가 본 또는 yet.The 루프를 전해 들었다플래시에서 public static var을 동적으로 생성하십시오.
for(var i:uint=0;i<22;i++)
{
public static var ("Name"+String(i+1)) = Texture(....);
}
루프에서 public static var을 생성 할 수 있습니까? 같은 뭔가 : 내가 본 또는 yet.The 루프를 전해 들었다플래시에서 public static var을 동적으로 생성하십시오.
for(var i:uint=0;i<22;i++)
{
public static var ("Name"+String(i+1)) = Texture(....);
}
아뇨는 메소드 정의 내에서 실행해야하는 당신은 방법 내부 public static var
를 선언 할 수 없습니다. 변수 이름도 동적 일 수 없으며 컴파일러는 컴파일 타임에 구문 오류를 확인해야하며 런타임에 변수의 이름을 지정하는 방법을 알 수 있습니까? 지역 변수의 동적 이름은 허용되지 않습니다.
Array, Dictionary 또는 Object을 사용하여 루프를 통해 하나 이상의 값을 저장할 수 있습니다. 그런데 루프 안에 public static
을 사용할 수 없습니다.
var myVarMap:Object = {};
for(var i:uint=0;i<22;i++)
{
myVarMap['Name' +i] = Texture(....);
}
는, 비록 나는 새로운 바보의 대답은 당신의 목적을 제공한다 정적 배열을 사용하여 생각하고는 더 읽기 논리를 유지하는 것입니다. 그러나 귀하의 질문에 대답하기 위해 클래스에 동적으로 public static 속성을 만들 수 있다면 : 예, 프로토 타입 객체를 사용할 수 있습니다. 아래 코드를 참조하십시오. 위의 코드에 대한
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
보통을, 아니 새로운 바보는 을 지적하지만이 도움이 될 수있다 접근한다.
실제로 ABC (액션 스크립트 바이트 코드) 조작 (예 : as3swf, as3-commons-bytecode)을 수행하는 라이브러리를 사용하여 일부 코드를 생성 할 수 있지만 이는 시나리오에 따라 복잡합니다. catholicon의 솔루션은 훨씬 간단하지만 동적 클래스를 사용할 때는주의해야합니다.
자산이있는 폴더가 있고이를 기반으로 자산 클래스를 생성한다고 가정합니다. 그것은 당신이 당신의 파일 시스템을 스캔 할 수있는 편안한 언어의 모든 종류를 사용해야 만합니다 : cmd/bash/c/C++/python/java 또는 javascript (플래시 IDE에서는 JSFL, 최소 AIR에서는 actionscript 3) 도구를 사용하여이 에셋 클래스를 생성 할 수 있습니다. 당신은 예를 들어 assetgen.py
으로 이것을 저장하면
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'
당신과 같이 실행할 수 있습니다 :
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');
}
}
예를 들어 Eclipse (예 : Eclipse/FlashBuilder/FDT와 같은 IDE에 통합 할 수 있음)를 사용하는 경우 프로젝트에서 애셋 변경/업데이트로 클래스를 다시 생성하는 작업을 쉽게 생성 할 수 있습니다.
자유롭게 변수 이름을 생성하고, 액션 스크립트 패키지를 지정하기위한 추가 명령 줄 인수 등을 추가하십시오.
위에서 언급 한 것처럼 위의 스크립트 (자산 디렉토리를 가로 지르고 문자열을 대체하여 actionscript 클래스 파일 생성)는 익숙한 언어로 작성 될 수 있습니다.