pureMVC assetLoader utility (3)

the main actors

the Loader Class:
this delegates loading tasks to its clients, and stores (if asked) the results.

the IAssetInfo Interface:
this is basically a value object that is requested from the Loader, on which properties or listeners may be set, and then passed back to the Loader to initiate loading.

the Clients:
these actually do the job of wrapping the different types of loading Objects (URLLoader, Loader, ModuleManager, Sound, NetStream… etc).

(if you are familiar with the Flex ModuleManager, the above implimentation is very similar).

the code
the refactored code can be downloaded from the googlecode svn here, or viewed by clicking the souce button on the demo below.

The loader is no means completed, but I feel that this is a good backbone to build on. It also needs some good thorough testing, but I’m confident that its basic functionality is sound.

[kml_flashembed movie=”http://www.revisual.co.uk/swf/assetloader_v2.swf” height=”500″ width=”470″ /]

To create and register the AssetLoaderProxy:

// create the LoaderProxy instance
var loaderProxy:LoaderProxy;
loaderProxy= new LoaderProxy();
// register the client that wraps 
// the flash.net.URLLoader Class
loaderProxy.registerClient(ClientTypes.URLLOADER_CLIENT, 
                           URLLoaderClient);
// register the client that wraps 
//the flash.display.Loader Class
loaderProxy.registerClient(ClientTypes.LOADER_CLIENT, 
                           LoaderClient);
// register proxy with the pureMVC framework
facade.registerProxy(assetLoader);

To retrieve an asset:

// create assetInfo
var assetInfo:IAssetInfo;
assetInfo= assetLoader.getAsset("filePath/myImage.jpg");
// test to see if it has already been loaded and stored
if(assetInfo.hasLoaded)
{
    trace("this asset has already been loaded");
    // retrieve and type asset;
    var myBMP:Bitmap = Bitmap( assetInfo.getAsset() );
}
else
{
    // add the id of the client that should 
    // handle the loading of the asset
    assetInfo.clientType = ClientTypes.LOADER_CLIENT;
    // add a LoaderContext or SoundLoaderContext if needed
    assetInfo.context = new LoaderContext();
    // add any listeners required
    assetInfo.addEventListener(Event.COMPLETE, 
                               completeHandler);
    // add the assetInfo to the load que
    assetLoader.addToQue(assetInfo);
}

The LoaderProxy that wraps the Loader class traps the Loader’s events and relays them as notifications to the pureMVC framework (see the enumerated type class LoaderNotificationNames).

2 thoughts on “pureMVC assetLoader utility (3)

  1. the answer is yes, of course, I have actually written a SoundClient… The whole point of writing it the way i have is that you can write new clients easily.

    are you actually using the Loader already? just because I’m still refactoring it, so there are minor alterations to the framework which will break previous versions, plus it hasn’t been fully tested.

    I have actually spoken to Chris Hall, and he has expressed an interest in including it as a pureMVC utility, so any “official” versions will hopefully be developed there. But I haven’t got it up to a standard yet.

    I’d be glad to get your thoughts and feed back.

Leave a Reply

Your email address will not be published. Required fields are marked *