ThinkNewDev
ThinkNewDev

Reputation: 668

What kind of class is embedded media in Flash Builder?

When you embed media in FLash Builder like below it creates a class to reference

[Embed(source="images/list.png")]
protected static const LIST_ICON:Class;

What kind of class does that create? And if I had a library swc that contained bitmapData, how would I go about in code creating the same kind of class using a bitmapData from my swc?

I have a SWC with with bitmapData

I have an xml file that comes in my file that defines what bitmap I am using

so I then get the class by doing

var ClassReference:Class = getDefinitionByName("ImageData") as Class;

"ImageData" is example for any text that the xml sends in... so it dynamically decides what swc class to fetch.

Afterwards I need to use this to create a Class that I guess that extends BitmapAsset containing the fetched bitmapData... thus emulating the embed from the first two lines above.

The end result is that I have a system that calls

getQualifiedClassName(SomeClass)

The class this is being passed to then uses

getDefinitionByName("passedName") 

to get the Class and thus the containing bitmapData.

Its a bit tricky but I have to do it that way to keep it dynamic on my end... can't modify the code that is handling the data passed to it. It has to be passed by name

I was hoping it would be simpler, but I am starting to think it would be easier to embed all of this in the normal way and make a simple switch that uses a different embed depending on what the XML says

Upvotes: 3

Views: 373

Answers (3)

alxx
alxx

Reputation: 9897

When you embed resource, it becomes new class in the swf. You want the name of this class. This is undocumented (afaik), but you can get it from parsed swf. For example, if I take my simple program TerrainGenerator.as, compile it and parse with SWiX, I see the following SymbolClass:

<SymbolClass>
    <Symbol Tag="0" Name="TerrainGenerator" />
</SymbolClass>

This is the main class, nothing unusual. Next, if I add Embed like this

public class TerrainGenerator {
    [Embed(source="SOME_PNG.PNG")]
    public static const IMAGE_RESOURCE:Class;
...

I will get the following:

<SymbolClass>
    <Symbol Tag="1" Name="TerrainGenerator_IMAGE_RESOURCE" />
    <Symbol Tag="0" Name="TerrainGenerator" />
</SymbolClass>

And if I move this Embed into class resource in package somepackage, it becomes:

 <Symbol Tag="1" Name="somepackage.resource_IMAGE_RESOURCE" />

So there are rules for generating those class names, and we can learn them with swf parser.

Upvotes: 0

Sly_cardinal
Sly_cardinal

Reputation: 13003

If you check out the documentation (http://livedocs.adobe.com/flex/3/html/help.html?content=embed_4.html) on the Embed metadata it shows that the image will be embedded with the type mx.core.BitmapAsset (that is, your LIST_ICON class extends BitmapAsset).

Adjusted for your embed, the code would be:

[Embed(source="images/list.png")]
protected static const LIST_ICON:Class;

...

var imgObj:BitmapAsset = new LIST_ICON() as BitmapAsset;

Check out the link above for the full code example.

Upvotes: 2

midhunhk
midhunhk

Reputation: 5554

You can create an instance of your image using var listIcon:LIST_ICON = new LIST_ICON();.

and you can assign this to an image's source as

image.source = listIcon;

Upvotes: 0

Related Questions