Question

In Flash Action script 3, when you need to load text, you use a class called URLLoader, and when you need to load an image (or .swf) you use a class called 'Loader.' As far as I know, loading a .bmp with URLLoader is as useless as loading an .xml into a Loader - it doesn't compute.

I'm making a class that handles a queue of external assets to be loaded - but aside from splitting the target URL to check out the file extension, I can't figure out a good way to tell if each URL requires a URLLoader or a Loader. At any rate, it's imaginable that a .php URL might return either an image or a document - so there's no way to count on the file names to dictate the right type of loader class to use.

Any ideas on how to reliably detect the right class for the job on a URL-by-URL basis?

Was it helpful?

Solution

well, the most tricky question, is determining the type of the target ...

  1. looking at the url is fairly simple, but may not always work ... some people serve images from phps and so on ...
  2. you could do it like browser ... start loading, and then look at what it is ... now again, there's multiple possibilities ...
    1. load the data as binary data ... when done, look at the starting sequence ... is it PNG (89 50 4E 47 0D 0A 1A 0A)? GIF (47 49 46 38 39 61)? JPEG (FF E0)? SWF ("FWS" (funny, isn't it?))? anything else should be text or text based ... in the case of an image, load it into a Loader with Loader::loadBytes ... be careful with SWF though ... you should only load graphicals SWFs like that ... in any other case, convert it to a String using the right encoding (ideally text data is served in utf8) ... then maybe you can already guess, whether it can be XML, JSON or URL-encoded variables ... try parsing (using classes XML, com.adobe.serialization.JSON, flash.net.URLVariables) ... if everything fails, it's probably just text (you can try to verify that superficially ... if you want some input on that, leave me a comment) ...
    2. do the HTTP yourself ... open a socket and load the source ... you will get mime-types in addition ... nothing you can rely on, but it helps ... there is an HTTP implementation in AS3 ... once you have the data
  3. pass the type manually ... pure and simple ... and you don't rely on anyone else ...

there is also an important difference between Loader and URLLoader ... Loader can load data accross domains, simply sandboxing its content so you cannot look into it ... URLLoader can only load from your domain, and domains that explicitely allow this using cross domain policy files ...

also, loading SWFs, that are not just external graphical assets, but you really want to interface with, you should not use this, since you need control over the LoaderContext etc. ...

so, yeah ... whatever way you choose, good luck ... ;)

greetz

back2dos

OTHER TIPS

Bulkloader does some guesswork based on the url, maybe take a peek in the source and see how they do it?

Personally, I would specify exactly how you would like to load you object if it is an image or xml. Since I have written a few queue loaders, I suggest that you dont just track a URL string but a set of objects with things like, the URL to load, if it has loaded, load priority, and type. Here is some psudo code.

class QueueObject{
  var URLtoLoad:String;
  private var hasLoaded:Boolean = false;
  var isDataObject:Boolean = false;
  var queuePriority:Number = 3; // 
}

Now in your code when you want to add something into your queue.

simply go

var loadObject:QueueObject = new QueueObject();
loadObject.URLtoLoad = "http://theurl.com/somedata.xml";
loadObject. isDataObject= true;

// Now push this into the awesome loader class that you have written which will manage the queue. I would send the object to load, the reference to the current scope eg, this, and the name of the function you want to call when this object has loaded.

MyStaticLodingClass.addQueueObject(loadObject, this, myFunctionThatYouWillCallWhenDone); 

That class above should now have an array which you can sort by priority to give you items to load, what their types are and their URL's. This way you can

This might help or not.

Other wise make something to figure out what type of object you are trying to load, eg BMP and use the correct loader based on a lookup for this object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top