Loading SWF from Flash CS5

During a work on a Flex 4.1 project, I tried to load a skins SWF file using the Loader class. I needed to use the inner UIMovieClips to embed in my Flex application.

I created a new Flash CS5 project (my_assets.swf), and in it added a movie symbol (my_movie). Then I converted this symbol to Flex movie using Flash’s “Convert Symbol to Flash Movie”.

To load it in Flex, I used the following code:

 

private function loadIt():void {

var myLoader:Loader = new Loader();

var myUrlReq:URLRequest = new URLRequest("my_assets.swf");

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);

myLoader.load(myUrlReq);

}

private function onLoaded(event:Event):void {

var myCls:Class = event.target.applicationDomain.getDefinition("my_movie") as Class

var myMovie:MovieClip = new myCls() as MovieClip;

}

 

Alas, when trying to run this code, I got the following error:

VerifyError: Error #1053: Illegal override of UIMovieClip in mx.flash.UIMovieClip.

 

It took some time to figure out what may have caused the error. When compiling the project in Flex 4.0, no error occurred, so I assumed this may be related to the version of UIMovieClip generated in the swf. I’ve looked everywhere trying to change its version to 4.1, but without success.

I’ve stumbled over the solution by mistake, after discovering the SWFLoader’s loadForCompatibility property suddenly allows me to load that SWF in Flex 4.1 with no errors whatsoever.

It seems that the versions are different between the Flash’s and Flex’s UIMovieClip, but there’s no reason to try to create a matching version. The main problem is that the UIMovieClip definition that is loaded from the SWF file tries to override the existing UIMovieClip definition. This wouldn’t have happened if the SWF was loaded in a sibling application domain and not in the current application domain.

One of the usages of sibling application domains is to allow code compiled in different Flex versions to work together. This way different definitions of UIMoveClip can coincide together. You can read more about application domains here.

So the solution is easy enough now – change the load command to add a loader context:

myLoader.load(myUrlReq, new LoaderContext(false, new ApplicationDomain()));

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com