A Full Stack Engineer's Perspectives on Technology, Startups, Software Engineering, and UI Design

AS3 / Flash: Introducing the AssetLoader Class

Posted: February 17th, 2008 | Author: | Filed under: Flash | Tags: , | 24 Comments »

In Actionscript 3, there are many different ways to load external assets from the Internet. Unfortunately, this can become cumbersome to find different ways to load each one in it’s own individual way. With this difficulty in mind, I have created the AssetLoader class.

Description

The asset loader class let’s you use the same method to load sound, video, pictures, swf’s, XML and CSS. The goal for this project is to make loading external assets as simple and clean as possible and give the user all the control they could every possibly need. And even better yet, this class is licensed with the M.I.T. license, so you can do whatever you want with it! Sweet isn’t it.

The only official download link:

Latest Release

AssetLoader_v1.2.zip

Archive

AssetLoader_v1.zip

Documentation (v1.0)

UPDATE: Preliminary Documentation: http://www.ayanray.com/assetloader/

Update Log

[March 10 2009] Uploaded latest revision that has support for stopping loaders, fixed memory leaks, and fixed a major bug that in some cases caused the same file to be loaded twice.

[July 25 2008] Uploaded documentation for v1.0


How Does It Work?

The AssetLoader uses a very simple concept. Items you want to load from the internet have extensions, and with these extensions, the AssetLoader can determine how to load the object. So, when you use the AssetLoader, you only need to pass the URL for loading to begin. Of course, simply loading an object is not enough. You must pass callbacks to listen to such events such as loading complete, progress events, and error messages. So what happens if you are trying to load an xml file that has a php extension? Well you can also explicity tell the AssetLoader what type of asset it is. So you could technically tell AssetLoader to load an image as a text file, and it will try to do it (and probably fail). Finally, the last key functionality of the AssetLoader is that it acts as a queue. It will load as many or as few as you want at any given time, and then when that download is complete, it will move to the next one in the queue. Set it to 1, and it will load one at a time until all the urls you passed to it are complete. Awesomeness.


What File types are Currently Supported?

Currently, XML, JPG, and Raw Text is supported. I have plans to add support for MP3, WAV, FLV, and other loaders, but because of school and work, I simply don’t have the time to make the final version. You can however EASILY add your own supported types and custom support by creating a new handler in the handlers folder in com.ayanray.loaders using the convention [FileExtension]Loader.as. AssetLoader will also automatically recognize your handler once you have added it there. Please, please contact me if you make your own handler because it is helpful for EVERYONE and that is why I am doing this, to help you.


What Callbacks are Supported?

Currently, the following callbacks are supported generally: onError (general use for almost anything that will stop it from loading your file), onComplete, onInit, onUpdate (File Download Progress), onTimeout, and onStart. This is not an exhaustive list, but is the most basic one. Each extension might have certain other properties that you can pass to the AssetLoader. For example, when loading an external SWF, you can assign an Application Domain. This is specific to the SWF loader in AssetLoader only and is not used in the XML. See the Documentation for more specific loaders.


Examples

For all examples, make sure you include the package ;) . com.ayanray.loaders.*

Example 1: Loading 1 Asset

[cc lang="actionscript"]new AssetLoader( http://www.ayanray.com/myimage.jpg,{onComplete: handleImageLoadComplete} );[/cc]

Example 2: Loading more than 1 Assets

[cc lang="actionscript"]var obj = {};
obj["http://www.ayanray.com/myimage.jpg"] = {onComplete: handleImageLoadComplete};
obj["http://www.ayanray.com/gallerylist.xml"] = {onComplete: handleXMLLoadComplete};
new AssetLoader( obj );[/cc]

Example 3: Queue Loader

[cc lang="actionscript"]AssetLoaderSettings.MAXLOAD = 1;
var obj:Object = {};
obj["http://www.ayanray.com/myimage.jpg"] = {onComplete: handleImageLoadComplete};
obj["http://www.ayanray.com/gallerylist.xml"] = {onComplete: handleXMLLoadComplete};
new AssetLoader( obj );[/cc]

Example 4: Loading XML with PHP extension

[cc lang="actionscript"]new AssetLoader( http://www.ayanray.com/xmlgenerator.php,{onComplete: handleImageLoadComplete, filetype: AssetLoaderTypes.XML } );[/cc]

Example 5: Stopping the AssetLoader

[cc lang="actionscript"]AssetLoader.stopQueued();[/cc]


Questions, Concerns, or Comments?

Please leave a comment if you find this is helpful! Feedback is always appreciated.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS