Last Updated: February 25, 2016
·
2.006K
· david_coleman

Accessing Embedded SWFs in Spark SWFLoader

When we embed an swf using

<s:SWFLoader id="foo" source="@Embed(source='foo/bar/file.swf')"/> 

One of the most frustrating things is accessing the class cast content inside.
Let's say we make an FLA, and we assign

com.example.component.BazzComponent 

to the document class of the FLA.

We want to work with the children of this component and we have properly assigned instance names and the like.

We compile the FLA to an swf and we are happy, so we embed it.
Now we find upon debugging and inspecting the loader that the foo.content property contains a big ugly ByteArray! Where is our swf?!

This is not as difficult as it seems!

Using the code shown below you can easily access the SWF via the main Document class, the trick is that you have to export the fla as an SWC to import the class definition and also embed the file in the loader as an SWF.

The rest is simple flex architecture, you have to access the content of the loader as a MovieClip. The first child of the MovieClip will be a loader that will load the real component. Access the content property of that loader and there you have the FLA document.

protected function get bazz():BazzComponent {
    var mc:MovieClip = contentLoader.content as MovieClip
    var loader:Loader = mc.getChildAt(0) as Loader;
    return loader.content as BazzComponent;
}

<s:SWFLoader id="contentLoader"
             source="@Embed(source='com/example/foo/assets/bazz.swf')"
             width="233" height="32" 
             right="0"
             y="1"
             trustContent="true"
             autoLoad="true"
             />