我有一个有很多嵌入式资产的课程.
在类中,我想按名称获取资产的类定义.我尝试过使用getDefinitionByName()和ApplicationDomain.currentDomain.getDefinition()但是都不行.
例:
public class MyClass { [Embed(source="images/image1.png")] private static var Image1Class:Class; [Embed(source="images/image2.png")] private static var Image2Class:Class; [Embed(source="images/image3.png")] private static var Image3Class:Class; private var _image:Bitmap; public function MyClass(name:String) { var ClassDef:Class = getDefinitionByName(name) as Class; //<<-- Fails _image = new ClassDef() as Bitmap; } } var cls:MyClass = new MyClass("Image1Class");
Marc Hughes.. 8
这不能解答您的问题,但它可以解决您的问题.我相信做这样的事情应该有效:
public class MyClass { [Embed(source="images/image1.png")] private static var Image1Class:Class; [Embed(source="images/image2.png")] private static var Image2Class:Class; [Embed(source="images/image3.png")] private static var Image3Class:Class; private var _image:Bitmap; public function MyClass(name:String) { _image = new this[name]() as Bitmap; } } var cls:MyClass = new MyClass("Image1Class");
我很难记住括号符号是否适用于密封类.如果没有,一个简单的解决方案是将类标记为动态.
这不能解答您的问题,但它可以解决您的问题.我相信做这样的事情应该有效:
public class MyClass { [Embed(source="images/image1.png")] private static var Image1Class:Class; [Embed(source="images/image2.png")] private static var Image2Class:Class; [Embed(source="images/image3.png")] private static var Image3Class:Class; private var _image:Bitmap; public function MyClass(name:String) { _image = new this[name]() as Bitmap; } } var cls:MyClass = new MyClass("Image1Class");
我很难记住括号符号是否适用于密封类.如果没有,一个简单的解决方案是将类标记为动态.