pureMVC assetLoader utility (1)

Yes, another asset loader utility, they seem to be the in thing at the moment, but none of the ones I’ve used are me. They are far too feature rich for my needs, clutter up the global namespace, and since I use pureMVC a lot, I may as have an asset loader utility that actualy is implimented in it.

This is the first version… a very early one, so much that I haven’t even writen any documentation for it. I just need to get it out there, and if not now, when?

At the moment it only loads non-streaming assets (ie those that are only available after Event.COMPLETE), and my aim, apart from those listed below, is to add features only when neccessary.

TODO:
1) write some documentation
2) impliment streaming file types (flvs, mpgs and zips)
3) refactor for use without pureMVC (maybe)

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

To create and register the AssetLoaderProxy:

var assetLoader:AssetLoaderProxy;
assetLoader = new AssetLoaderProxy();
facade.registerProxy(assetLoader);

To retrieve an asset:

var asset:IAssetContainer;
asset = assetLoader.getAsset("filePath/myFile.jpg")

The AssetLoaderProxy will return an IAssetContainer which either contains the asset (if it has been previously loaded and stored) or the information that will enable the loading of the asset. So the first thing to do is to check to see its loaded state:

if(asset.hasLoaded)
{
    trace("this asset has already been loaded")
}
else
{
    trace("this asset has not been loaded yet")
}

If the asset has already been loaded, it can be accessed either via the getAsset():Object method, or using any of the pre-typed getters.

To load an asset:
If the asset has not already been loaded, listeners, loader contexts and other properties can be added to the IAssetContainer, before it is passed back to the AssetLoaderProxy load que.

asset.addEventListener(Event.COMPLETE, completeHandler);
asset.addEventListener(ProgressEvent.PROGRESS, progressHandler);
asset.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
assetLoader.addToQue(asset);

Note on Typing:
The loaded assets will be typed as its loader types it. For example, an XML file is loaded with a URLLoader and is therefore a String. At no point will this String be parsed as an XML within the AssetLoader utility until you call the getAsXML() method from the IAssetsContainer. The string will then be parsed and stored as an XML. If you want to keep it as a String, always call the getAsset() and type it, or call getAsString(). This is a good thing, as it gives you more control over it (ie changing whitespace settings before parsing).
The type of asset can be ascertained through its fileType property, which is basically just the file extension.

Notifications:
The AssetLoaderProxy also sends pureMVC Notifications. You can use its enumerated static constants to register commands that will respond to these.

facade.registerCommand(
                        AssetLoaderProxy.LOAD_QUE_PROGRESS, 
                        WriteToProgressBar
                       );

The AssetLoaderProxy instance will be passed as the body of the Notification, so any information required can be taken from its public properties.

And finally:
So, that gives a very quick over view. The next thing I’ve got to do is commenting documentation, and perhaps some UML diagrams.

Update:
I Have created a google code account, for this and other pureMVC utilities. I intend to get a fully featued download out at the end of July.

continuous improvement

Well, I’ve just completed another project – a flex RIA for the BBC. A month ago, I would have been proud of it… but in writing it, as always I’ve learned more, and just want to rewrite it all again.

to be more precise I found this article
http://www.adobe.com/devnet/flex/articles/client_perf_04.html

Deep nesting of containers slows down ui creation. Careful planning of view components can eliminate these. Also, I would probablely create modules for my main view elements, and load them in with their own Mediators (i’m using multicore pureMVC).

pan and zoom

well, I thought i’d spend some time rewriting some of my old as2 components in flex, and here are the results of the first.

I had attempted to do this about six months ago, but got terribly stuck. this time it took hardly any time at all, as I used my new found knowledge of BitmapData and Matrix transformations (a nod to plugin media for the intensive course:~).

the binding goes both ways, drag the main view or the dingo.

[kml_flashembed movie=”swf/pan_and_zoom.swf” height=”300″ width=”480″ /]

Flash Back

After a year of working almost exclusively in ActionScript 3 and Flex, a client requested that I update the website I created for him when ActionScript 2 first came out.

I spent a whole day of absolute frustration.

I tried going back to SEPY as my editor, but it just made all my beautiful code look hard, cold and ugly – it hurt my eyes after a few minutes – I just couldn’t continue. The look and feel of working with flex just makes my insides all warm and cosy – I needed that feeling!

So I opened up Flash and yes, my code again had that nice crisp softness it needed. But every time I tried to type a variable, Flash would substitute a Class at random, unless I clicked my mouse first.

Then it expected me to type out all the imports myself (the cheek)!; and no matter how much I control clicked my class/property/methods, Flash refused to hot-link me through to their declarations. So I had to tediously navigate my own way through my .as files and the code within them.

I’ve used Flash daily for eight years – now it is completely foreign to me. Its like meeting an old girlfriend and wondering how in your right mind you ever fancied her, let alone stay with her for all that time. I never realised how much I have become attached to Flex, until now.

Now we come to the real issue. I spent that whole day trying to figure out why I was getting a type coercion error on a class that obviously and categorically implemented the correct interface. Only it turns out that in ActionScript 2 a class doesn’t inherit its super’s interface, you have to re-implement it on the subclass. Now that might have been a piece of knowledge that I took for granted a year ago, but now… for godsake why?

What other nightmares were going to assail me when I try to do something a teeny weeny bit complicated? I decided that it was probably quicker to rewrite the whole app from scratch using ActionScript 3 in Flex – and honest to god, it will do too.

[SWF()]

Well, I spend the best part of last night searching the flex documentation and adobe support for a referance to the SWF metadata tag. Now I knew such a thing existed (i can’t remember how I knew) but I needed to know the correct syntax and legal arguments. Could I find anything? could I buggery!

Grrrrr!! Why is this not documented!!

I finally found a post as3 projects and the swf metadata tag on trace(this).com that referenced the AS3 Cookbook (which is where I must have seen it originally).

for posterity here it is:

[SWF(width=’400′, height=’300′, backgroundColor=’#ffffff’, frameRate=’30’)]

Derek Vadneau says that it must come before class declaration and after the imports