views:

210

answers:

2
+1  Q: 

array of loaders?

How does one create an array of loaders?

The bigger picture:

I have written a mapping program in flex. I want to change my mapping program so that all I need to do is drop in a new xml file instead of going into my flex file and adding the exact number of item loaders I need. So I guess I'm looking for an array of loaders that can load the image files that are within my XML file.

xml file example:

<locations>
      <location>
        <name>Big Star</name>
        <street>123 Some St.</street>
        <city>City</city>
        <state>XX</state>
        <zip>555555</zip>
        <lat>12.34567</lat>
        <long>-67.54321</long>
        <iconFile>bigStar_icon.gif</iconFile>
        <imageFile>bigStar_img.swf</imageFile>
        <motion>no</motion>
        <featured>yes</featured>
        <category>Grocery</category>
      </location>
</locations>

this xml can sometimes have 2000 locations.

+1  A: 

Yeah, that's a pretty bad question. Literally (if facetiously):

var loaders:Array = [new Loader(), new Loader()];

Trying to read into what you asked, it sounds like you are trying to load up a whole bunch of stuff and handle them all loading together somehow... and if you have to ask, you're going to struggle.

You could try using the Bulk Loader library though, which does this for you reasonably well. There are probably others.

alecmce
I'm trying to create loaders that load swf's from a filename specified in an xml, so I don't know how to create all those loaders. Is there a better way to approach it?
Phil
You mean like a loop?
Anon.
I am trying to do it in a loop right now yes.
Phil
+1  A: 

Not a direct answer to the question since it's a bit vague + @alecmce already gave an answer (I would go for a loader queue such as BulkLoader in this situation as well).

However, since I noticed a similar question some time ago I just wanted to point out that instantiating all the loaders at once just feels a bit wrong.

Wouldn't it be a bit appropriate to just store the urls and handle process them one by one ?

Basic example : (beware I typed it in without testing...)

// So here's the point: only Strings are stored ... 
var urls:Array = new Array('image1.jpg','image2.jpg',image3.jpg);  

loadNext();

function loadNext()
{
    if(urls.length() == 0)
        return;

     load(urls.shift())
}

function load(url:String):void
{
    // The loader is created lazily just before before we need it
    var loader:Loader = new Loader(new URLRequest(url));
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded)
    loader.load(url);

}

function onLoaded(e:Event):void
{
     event.target.removeEventListener(Event.COMPLETE, onLoaded);
     addChild(event.target.content); // ... or whatever has to happen here.
     loadNext();
}
Theo.T
I edited my original post to provide a little more info.
Phil
It's an interesting approach Theo, but I'd caution against it. If you load something using Loader then remove it's contents into a different display object you're going to have a hard time removing it from memory again.
alecmce
You also lose the benefit of being able to add the Loader to stage itself and let the asset appear when the asset is ready, which means you add a race condition into your code. Let's suppose you load in a bunch of planets and want to position and size them, but the position-and-scale logic doesn't wait for the loading cycle to complete... if all the planets are stored in loaders, then you can target the loaders and position and scale them, even before the content has loaded.It's not a wrong approach, just something to think about!
alecmce
Well right now I have a single function that creates ALL loaders and loads them without waiting on a COMPLETE.
Phil
@alecmce - Yup I agree. I always use loading managers so I don't have to worry to much about memory management. Still I really tend to never initialize an object unless I need them directly (lazy initialization) as thumb rule. In case there are DisplayContainers to be "filled" with to-be-loaded objects I use to have getters providing 'asset Proxies' to be handled by the loading managers. However this is ways to framework specific and in this example I just wanted to give a hint to a different approach... I guess it's all a bit personal in the end : ) Thanks for pointing it out, T.
Theo.T