Runtime Asset Loading with Closure

The last scene in Inception was rough for me. I'm a 'closure' kind of guy I guess... even in code.

It's been a while since Ben Stuki graced us with his IconUtility class; a class that facilitates runtime asset loading. Why would you want to load assets at runtime? Good question...

I suppose we can boil it down to needs like: reducing application load time, resolving unknowns (e.g. plug-ins), modularization, and UI customization. There are many more I'm sure... but this is a good start.

Fast forward a couple months...

Mike Chamber's post on Functions, activation objects and 'this' in ActionScript 3 was the spark. The original IconUtility solved about 90% of the use-cases mentioned but some controls were so finicky that I finally decided to see if I could refactor Ben's approach and add a caching mechanism and some choice in how the asset was handled once it was loaded.

Anyway...

My approach uses closures that pass assets to a style, method, property, or can be pre-loaded and placed in a cache for later use. Enjoy!

  1. package com.quilix.loader
  2. {
  3.        
  4.   import flash.display.Bitmap;
  5.   import flash.events.Event;
  6.   import flash.events.EventDispatcher;
  7.        
  8.   import mx.core.BitmapAsset;
  9.        
  10.   public class AssetUtility extends BitmapAsset
  11.   {
  12.                
  13.     private var assetLoader:AssetLoader = AssetLoader.getInstance();
  14.     private static var _path:String = "";
  15.                                
  16.     public static function getClass( path:String ):Class
  17.     {
  18.       _path = path;
  19.       return AssetFactory;
  20.     }
  21.                
  22.     public function AssetUtility():void
  23.     {
  24.       addEventListener( Event.ADDED,
  25.                                
  26.       function(event:Event):void {
  27.                                        
  28.         ( event.target as EventDispatcher ).removeEventListener( event.type, arguments.callee );
  29.         iconLoader.load( _path,
  30.                                                
  31.         function( bmp:Bitmap ):void {          
  32.           bitmapData = bmp.bitmapData;
  33.                                        
  34.           if ( parent.hasOwnProperty("invalidateDisplayList") == true )
  35.             Object( parent ).invalidateDisplayList(); }, "", AssetLoader.METHOD );
  36.                                        
  37.           }, false, 0, true );
  38.         }
  39.                
  40.       }
  41.     }

How is the magix accomplished? You'll need the AssetLoader.as class (attached below) to make it happen; additional documentation / examples are included within.

Just keep in mind that AssetUtility isn't meant to be a drop in replacement for IconUtility and will likely require some mental re-adjustment - e.g. the takeaway from this post revolves around using a closure in lieu of an external event handler when loading assets at runtime.

If this post didn't help you forget the angst as the last scene of Inception faded to black... perhaps this will.