Loading an SWF and getting components from its library

Keeping all assets in one SWF causes problems while updating assests and compiling times are longer. Nowadays only crazy people add everything into a one SWF and the Flash platform has improved a lot to support alternative ways to embed, load and add assets.

The flash.utils.getDefinitionByName allows to load assets from the library of another SWF-file. I’ve simplified the process in my Utils.as and here is an example how to use it.

Main thing is to use the ApplicationDomain to keep the loaded SWF in its own domain. ApplicationDomain allows multiple definitions of the same class to exist and allow children to reuse parent definitions.


//load the swf while keeping its ApplicationDomain

var request:URLRequest = new URLRequest(PATH_TO_SWF);
var loader:Loader = new Loader();

var context:LoaderContext = new LoaderContext();
context.applicationDomain = new ApplicationDomain();

loader.load(request, context);

//after the SWF is loaded, use my Utils.getAsset to get an asset from the loaded SWF

var mc = Utils.getAsset("mc", swf);

addChild(mc);

Download the source code and example files.

Kommentoi