How to access images stored in a SWF file by class name


#1

For a HTML5 project I have a bunch of images stored in a SWF file. Each one is linked to a class name. They are not in any symbol so they do not have instance names, which means I cannot use Relect.field.

In Flash actionscript I could get an image by class name using the following:

private function getImage(imageClassName:String):Sprite
{

 var image:Sprite = null;
 var imageClass:Class = getDefinitionByName(imageClassName) as Class;
 if (imageClass != null )
     image = new imageClass() as Sprite;

 return(image);

}

I researched this online but any solution I found I didn’t understand or was overly complicated, usually assuming that the constructor for the desired class would need parameters passed, etc.

What is the equivalent function in OpenFL/Haxe?

Thanks in advance!


#2

If the BitmapData is linked to a class in the SWF, use Assets.getBitmapData, if it is linked to a MovieClip, use Assets.getMovieClip, last I tested, you cannot link a Sprite to a class within a SWF.

…but this might be what you need:

var bitmapData = Assets.getBitmapData ("library:MyImage");

#3

This is the symbol properties for MyImage:

ImageProperties

This works: var image = new MyImage();
This bombs the program: var image = Assets.getMovieClip(“library:MyImage”);

Any get command I tried including getBitmapData had same effect. getMovieClip(“MyImage”); also doesn’t work.

var list:Array=Assets.list(AssetType.MOVIE_CLIP); gives me an empty array.

In my test SWF file I have a total of 4 movieclips defined as shown above. Why doesn’t Assets.list show them?

It seems to me that even though the SWF file is in the assets folder, the Assets class doesn’t look inside the SWF file.


#4

What does your XML look like? Is it being included in a library called “library”, or something else? If you don’t add a <library path="" id="" />, the default ID value matches the filename, so my.swf would need Assets.getMovieClip ("my:MyImage");