Jump to content
Search Community

Modifying LoaderVars after parse but before load

alanpuccinelli test
Moderator Tag

Recommended Posts

So I'm loading a series of loaders using XML and I'm curious... Can I modify the loader vars after I parse the XML (create the loader objects) but before I call the load() method?

 

for example:

 







 

var ldr1 = LoaderMax.getLoader("ss1");

//do something here to add the onChildComplete method to ldr1  ---  like getLoaderVars? and then edit it's properties?

ldr1.load();

 

I realize I can just add it to the XML but I'd like to try to keep my XML minimal if possible and it seems highly repetitive to set all those common vars for each of the imageloaders. Is my only other option to just use plain xml with the filenames and then programmatically iterate through the nodes and build the loaders for each? I'm making a slideshow and pretty much everything has the same width, height, crop, and scaleMode properties but the XML file is way bigger than it needs to be if I have to repeatedly set the vars for each loader. There's got to be a better way.

Link to comment
Share on other sites

Upon further examination it looks like the parse() method could do what I want if I parse the xml nodes to an array first, but if my array is mixed assets that will create different loaders how do I pass the appropriate childVars to each of the loaders.

 

So in this example:

LoaderMax.activate([imageLoader, SWFLoader, XMLLoader, MP3Loader]);

var urls:Array = ["img/photo1.jpg","../../xml/data.xml","swf/main.swf","http://www.greensock.com/audio/music.mp3"];

//now parse all of the urls, creating a LoaderMax that contains the correct type of loaders (an ImageLoader, XMLLoader, SWFLoader, and MP3Loader respectively)
var loader:LoaderMax = LoaderMax.parse(urls, {name:"mainQueue", onComplete:completeHandler}) as LoaderMax;

//begin loading
loader.load();

 

How do I set the appropriate children vars for each of the different loader types?

Link to comment
Share on other sites

Sure, this is very possible in LoaderMax. Here's what I'd recommend:

 

You can keep your XML pretty simple (strip out the common properties that all the ImageLoaders share) like this:






 

Then, once the XMLLoader inits or completes, you can loop through the child loaders and add the width/height/scaleMode/crop stuff directly on the ContentDisplay objects associated with each loader (their "content"). Remember, when an ImageLoader is created, it immediately creates a ContentDisplay object into which the rawContent will load. This is very convenient because it allows you to put stuff onto the stage wherever you want, add listeners, etc. even before you start loading the raw content. The constructor's "vars" parameter (where you normally pass in the width/height/scaleMode/crop special properties) is generally only intended for use with the constructor, so it doesn't do any good to do something like loader.vars.width = 500 after the ContentDisplay is created. Instead, alter the ContentDisplay's properties directly. The only tricky part is making sure you use the "fitWidth" and "fitHeight" properties instead of "width" and "height" if you want to define stuff for the scaleMode. So here's what your code might look like in the initHandler() for your XMLLoader:

 

function initHandler(event:LoaderEvent):void {
   var queue:LoaderMax = LoaderMax.getLoader("ss1");
   var loaders:Array = queue.getChildren();
   for (var i:int = 0; i         if (loaders[i] is ImageLoader) {
           var content:ContentDisplay = loaders[i].content;
           content.fitWidth = 1024;
           content.fitHeight = 576;
           content.scaleMode = "proportionalOutside";
           content.crop = true;
       }
   }
   queue.load();
}

 

Make sense?

Link to comment
Share on other sites

Yes that makes perfect sense thank you! I'd been trying to apply those properties to the loader and not the ContentDisplay.

 

Also you taught me something new about identifying a class, believe it or not I never new about the "is" operator before. Duh. Way simpler that using "getQualifiedClassName" which is what I've been doing up until now. You learn something new every day. Thanks so much.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...