AssetLoader

AssetLoader is a simple to use library that allows you to load multiple assets at the same time. The aim is to make preloading assets in AS3 as simple as possible, but also give you the functionality and extendibility you need.

Download

The latest resource bundle include: source code, swc, unit testing source, examples and ASDocs. You can also download/watch/fork AssetLoader on GitHub.

Links

Some Features

  • Automatic Type detection.
  • Multiple connections, which you can define = faster loading = less waiting = happier users.
  • No Events, AS3Signals only.
  • No singletons! Construct as many AssetLoaders as you want!
  • Data is dispatched with strong types. NO CASTING!
  • Endless recursive group loaders within loaders, within loaders, etc.
  • Pause/Resume/Destroy capabilities.
  • Dynamic parameter system that works with code completion – No guessing!
  • Loader parameters smartly inherit from their parent loaders, which means you write less code.
  • Real-time loading stats. This provides latency, speed, average and progress of all downloads.
  • Consolidated stats, loaders reclusively consolidate loading stats all the way down to the last child.
  • Can be configured via XML, straight up or from an external file with one easy call.
  • All loader errors are consolidated to one signal. IOError, SecurityError, etc. – you don’t have to worry about it, they all boil down to one place. Which means you can “globally” handle loading errors with one listener/command.
  • Interfaces all round.
  • Unit tested!
  • Works super well with Robotlegs! //what doesn’t
  • And some other stuff too!

Understand the concept

I must admit, version 1 of AssetLoader was a bit confusing, but paved a good runway for version 2. Before we continue, please make sure you have read the README on the sources page. Getting on with the concept:

AssetLoader is a cluster of well-oiled classes that focus on making loading assets into Flash as easy as possible. Yes, there are alternatives, but I have yet to see any of the other ones that implement good programming standards and interfaces. Also v2 is fully unit tested to make sure that coming updates don’t break anything.

AssetLoader works on a Child-Parent notation, much like the display layer works. Information is always bubbled upwards to the top level loader, making it easy to “globally” manage errors and completed assets. More on this later…

ILoader – the Work Horse (you and me)

This is the base interface for all loaders, including the AssetLoader itself. An ILoader instance is responsible for loading and managing one asset/file. It wraps & listens to its inner native loader (e.g. URLStream, Sound, NetStream, Loader) and converts the native Events into Signals, updates its Stats module, etc. All these implementations can be constructed and used independently as well; they do not need a parent to function.

IAssetLoader – the Manager (our bosses)

Currently there is only one implementation of the IAssetLoader, namely AssetLoader. At this point only one is needed. IAssetLoader is the one that can contain an infinite amount of child ILoaders, being an ILoader itself; it can also contain instances of itself (IAssetLoaders). IAssetLoader doesn’t wrap or contain any native loaders, it merely manages and oversees its children, but because everything is consolidated it can function as a standard ILoader.

Credits

  • Pingback: ActionScript Webinar - AssetLoader with Robotlegs & Unit Testing | RIA Development with Adobe Flex and Flash

  • Dave Hulting

    Matan, This is a fantastic class, and I’ve upgraded to v2. I’m trying to load a compiled font swf and register it to the Font.registerFont, but I do not know how to get the applicationContext from the new signals in v2. I’m trying to convert the following to signal-friendly:
    private function var FontLibrary:Class = event.target.applicationDomain.getDefinition(“_Arial”) as Class; Font.registerFont(FontLibrary._Arial);

    My font swf is loading & I can get the data parameter (the actual _Arial Class), but it’s something with the applicationDomain.getDefinition. I obviously don’t understand it enough. I thought writing another loader with events would be to verbose, but I might just have to do that.

    Perhaps another AssetType of FONT? I may try to extend, or just get ‘er done with the Event.COMPLETE event within the class.

    Thanks,
    Dave

    • http://doesflash.com Matan Uberstein

      Thanks Dave,

      Creating a Font AssetType sounds like a good idea, I’ll look into it. :)

      Was

      event.target.applicationDomain.getDefinition

      working before? Seems weird because you need to get the applicationDomain from the loaderInfo object. E.g.

      event.target.loaderInfo.applicationDomain.getDefinition

      I’ve quickly made you a small example:
      Main.as

      package
      {
      	import org.assetloader.core.ILoader;
      	import flash.text.TextFieldAutoSize;
      	import flash.text.TextFormat;
      	import flash.text.TextField;
      	import org.assetloader.loaders.SWFLoader;
      	import org.assetloader.signals.LoaderSignal;
      
      	import flash.display.Sprite;
      	import flash.net.URLRequest;
      	import flash.text.Font;
      
      	public class Main extends Sprite
      	{
      		public function Main()
      		{
      			var loader : ILoader = new SWFLoader("fonts", new URLRequest("fonts.swf"));
      			loader.onComplete.add(complete_handler);
      			loader.start();
      		}
      
      		protected function complete_handler(signal : LoaderSignal, content : Sprite) : void
      		{
      			var font : Class = Class(content.loaderInfo.applicationDomain.getDefinition("fonts_Arial"));
      			Font.registerFont(font);
      
      			var txt : TextField = new TextField();
      			txt.defaultTextFormat = new TextFormat("Arial", 20);
      			txt.autoSize = TextFieldAutoSize.LEFT;
      			//setting rotation to 5, because a NON-embeded font will not render.
      			txt.rotation = 5;
      			//set to true and false to see difference, if rotation and embedFonts = false, text will not render.
      			txt.embedFonts = true;
      
      			txt.text = "Hello!!! This is an embeded font!";
      
      			addChild(txt);
      		}
      	}
      }
      

      Here is the Font.as which is being compiled into fonts.swf.

      package
      {
      	import flash.display.Sprite;
      
      	public class Fonts extends Sprite
      	{
      		[Embed(source="C:/path/to/font/arial.ttf", mimeType="application/x-font-truetype", fontName="Arial",  unicodeRange="U+0020-U+002F,U+0030-U+0039,U+003A-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E,U+02C6,U+02DC,U+2013-U+2014,U+2018-U+201A,U+201C-U+201E,U+2020-U+2022,U+2026,U+2030,U+2039-U+203A,U+20AC,U+2122", embedAsCFF="false")]
      		public var Arial : Class;
      
      		public function Fonts()
      		{
      			//Trace to see that the packge/class will be of our font.
      			//trace(getQualifiedClassName(Arial));
      		}
      	}
      }
      

      You can download the working example here: http://bit.ly/fMOnN1

      Hope this helps!

      • Dave Hulting

        Matan,
        Thanks for the example and direction! I now have my fonts embedded and are able to call them just as I would if they were compiled in the swf.

        Thanks again for a great class and your help! Keep up the great work.

  • am am

    hi Matan!
    Is there any specific place for feature request? =PI was looking for a way to have a alternate url, something that could toggle to when the max number or retries is reached. What do you think about this? :]

  • Pingback: [转]AS3 Code Libraries (APIs) | 南龙的小站

  • Pingback: AS3 经典的代码库 | badyoo RIA技术博客 AS3 flash adobe AIR

  • Pingback: AS3 Class Library