Essentially the AssetManager will execute loads, manage the queue and dispatch an event when its complete. It should also be the only place where a getDefinitionByName is called. I won't post the whole file, I'm hosting them at GoogleCode for that reason. Here is the AssetManager in the source browser though.
The only bit worth mentioning is the load function :
public function load(toLoad : Object) : void { if (toLoad is Asset) { queueAsset(Asset(toLoad)); } else if (toLoad is Array) { for each (var asset : Asset in toLoad) { queueAsset(asset); } } if (!_loaderInProgess) loadNext(); }
You can pass it either an Asset or an Array of Assets which will then be added to the queue, if nothing is loading it will then start the load process with the loadNext() call.
The AssetManager uses to the AssetLoader to load each Asset. It's a pretty simple class, all it does is instantiate the loader type it requires, adds / removes listeners and throws and errors if it encounters them. To prevent it being garbage collected it passes itself to the Asset.
Both classes will accept and handle on an Asset object or multiple Asset objects. Again not much to report here but the Asset class is more a data type. It stores all the information required to load and access the content. The only complicated doohickey here would be the isData() getter :
internal function get isData() : Boolean { var type : String = _urlRequest.url.substr(_urlRequest.url.length - 4, 4); switch (type) { case ".swf" : case ".png" : case ".jpg" : case ".gif" : return false; default : return true; } }
This works out from the URL what type of loader to use. Great eh?
So it's pretty easy to use, you create an instance of the AssetManager, add an event listener so you know when it's complete and pass it a load of Assets to load ::
public function AssetManagerTest(stage : Stage) { _stage = stage; _stage.addChild(new Profiler()); _loadList = [ new Asset("spritesheets/explosion.xml"), new Asset("spritesheets/explosion.png"), new Asset("spriteclips/explosion.swf") ]; _assetManager = new AssetManager(); _assetManager.addEventListener(Event.COMPLETE, loadComplete, false, 0, true); _assetManager.load(_loadList); }
To access the content you do so pretty much like you would with a loader, with the content property :
private function loadComplete(e : Event) : void { _updateProgressTrigger.stop(); var loadedXML : XML = new XML(_loadList[0].content); var loadedBitmap : Bitmap = _loadList[1].content as Bitmap; var loadedLinkedLibrary : Class = _assetManager.getClass("Explosion"); var loadedLinkedMovieClipInstance : MovieClip = new loadedLinkedLibrary(); trace("[AssetManagerTest.loadComplete] |TEST| XML :: " + loadedXML.toXMLString()); trace("[AssetManagerTest.loadComplete] |TEST| Bitmap :: " + loadedBitmap); trace("[AssetManagerTest.loadComplete] |TEST| MovieClip :: " + loadedLinkedMovieClipInstance); }
You might wonder why I bothered with this? It's basically to wrap the entire loading process into a nice little bundle which I can use whenever I want. I may make this a singleton class so it can be accessed anywhere but I'd rather managed the access to it. It also means if I need to update the way I load things I can do it one place.
Right I've uploaded a downloadable zip which has all of these classes in them and a test swf so you can see the result. Let me know if you have any problems (if in fact anyone actually reads this!).
I'm sure loads of it will get updated as I progress along the project work flow (there isn't one yet :]). Pretty basic stuff. Next post I'll get into the blitting package and hopefully that will be more interesting.
No comments:
Post a Comment