views:

2862

answers:

3

I have a class with many embedded assets.

Within the class, I would like to get the class definition of an asset by name. I have tried using getDefinitionByName(), and also ApplicationDomain.currentDomain.getDefinition() but neither work.

Example:

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");
+1  A: 

This doesn't answer your question, but it may solve your problem. I believe doing something like this should work:

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");

I'm having a tough time remembering if bracket notation works on sealed classes. If it doesn't, a simple solution is to mark the class as dynamic.

Marc Hughes
Hey! Nice job, that worked!
A: 

You don't need to use any fancy getDefinitionByName() methods, simply refer to it dynamically. In your case, replace the 'Fails' line with:

var classDef:Class = MyClass[name] as Class;

And that should do it.

davr
A: 

Thank you so much! I just spent almost 5 hours trying to get the POS getDefinitionByName to work with the getQualifiedClassName that I was ready to throw stuff!! My final working code looks like this and even gets the string name from an array.

CreatureParam is a 2 dimentional array of strings;

Type is an integer that is sent to flash by HTML tag which is turn comes from a MYSQL database via PHP.

Mark1_cb is a combobox that is on the stage and has an instance name. It's output is also an integer.

So this code directly below imports the class "BirdBodyColor_mc" from an external swf "ArtLibrary.swf". BirdBodyColor_mc is a movieclip created from a png image. Note you must double click on the movieclip in the ArtLibrary.fla and insert a second key frame. Movieclips apparently need two frames or flash tries to import it as a sprite and causes a type mismatch.

[Embed(source="ArtLibrary.swf", symbol="BirdBodyColor_mc")] var BirdBodyColor_mc:Class;

Normally I would put an instance of this movieclip class on the stage by using this code.

myMC:MovieClip = new BirdBodyColor_mc(); addChild(myMC);

var Definition:Class = this["BirdBodyColor_mc"] as Class; var Mark1:MovieClip = new Definition();

But I need to do this using a string value looked up in my array. So here is the code for that.

var Definition:Class = this[CreatureParam[Type][Mark1_cb + 2]] as Class; var Mark1:MovieClip = new Definition();