Standardizing runtime asset loads (incl. Runtime fonts!).

I’ve been looking for better ways to reduce the size of my main app swfs. Loading assets at runtime has proven to be a flexible solution that saves size in the initial load and throughout the use of the app, as only files essential to the current view can be loaded in. I found myself repeating code a lot for the runtime load process, so I’m working on a way to standardize getting external assets into my Flash app.

Check out my RuntimeLibraryItem class. This abstract class can be easily extended or instantiated for different types of external assets or just individual items.

For example:
Externalizing my fonts saves huge weight up front, but loading and registering them can be a bit confusing and tedious. My RuntimeFont class is an extension of the RuntimeLibraryItem class and streamlines the external font loading process.

I’ve started to build a library of font swfs for each project by generating a swf for each font with a single exported font library item. So, LucidaGrandeRegular.swf would contain the exported font symbol, ‘LucidaGrandeRegular’. The class LucidaGrandeRegular.as would then look like this:

public class LucidaGrandeRegular extends RuntimeFont
{
        public function LucidaGrandeRegular(autoLoad:Boolean = false)
        {
		var url:String = 'path/to/LucidaGrandeRegular.swf';
		super('LucidaGrandeRegular', url, autoLoad);
	}
}

RuntimeFont takes over from there:

public class RuntimeFont extends RuntimeLibraryItem
{
        public static var library:Array = new Array();

        private var _name:String;

        public function RuntimeFont(name:String, url:String, autoLoad:Boolean = false)
        {
                _name = name;        // save the name of the font to be registered
                super(url, autoLoad); // RuntimeLibraryItem class loads the swf
                library.push(this);     // store this in a static var of runtime fonts
        }

        private function registerFont():void
        {
                // registers with the Font class for use in the application
                Font.registerFont( getClass(this.name) as Class );
        }

        override protected function handleLoadComplete(event:Event):void
        {
                this.registerFont();  // register the font when the swf loads
                super.handleLoadComplete(event);
        }

        public function get name():String
        {
                // utility getter for whatever
                return _name;
        }

}

This turns the font load for LucidaGrandeRegular into something as simple as

var lg:LucidaGrandeRegular = new LucidaGrandeRegular();
lg.addEventListener(RuntimeClassLibrary.LOAD_COMPLETE, handleFontsLoaded);
lg.load();

This is still evolving, but I’ll post sample code in the examples directory soon.

Comments

One comment

<-->

The Outspoken

  1. July 29, 2009 | 07:51

    [...] Andy Hatch has been working on utility classes for runtime asset loading in Flash, specifically runtime fonts. Runtime fonts are a particularly tricky thing to get right, but this looks like a clean, simple [...]