04-30-2009
Actionscript 3: Using External Assets
By using an external assets swf file, I have been able to drastically reduce the file size of my main swf file. In my external asset swf file I include all fonts and audio(sound effects). To do this, I create a swf file and make sure all of the assets have Linkage with a class name, for example a font that is used a lot, I might call “MainFont.” Once all assets are properly exported for Actionscript, Publish the swf file, and let the main swf know where it is:
private function loadAssets():void
{
_loader = new Loader();
try
{
_loader.load(new URLRequest("assests.swf"));
}
catch(e:Error){}
_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, io, false, 0, true);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, assetsLoaded, false, 0, true);
}
private function assetsLoaded(e:Event):void
{
e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, io);
e.currentTarget.removeEventListener(Event.COMPLETE, assetsLoaded);
var app:ApplicationDomain = e.currentTarget.applicationDomain;
try
{
Font.registerFont(app.getDefinition("MainFont") as Class);
}
catch(e:Error){}
try
{
_loader.close();
}
catch(e:Error){}
_loader = null;
}
private function io(e:IOErrorEvent):void{}
Once the swf file is loaded into your Application, you must use the ApplicationDomain of the loaded swf to access all classes in it. By using Font.registerFont(), I am able to add the font from the loaded swf file into my main Application and access it from anywhere.
Nice post man, this is very useful info I’ve never thought of having fonts in an external swf.
Thanks
I have no idea what you are saying, but I think you are pretty smart!