Jump to content
GreenSock

bburwell

Delayed getContent call

Recommended Posts

Howdy Jack.

 

I have a feeling I'm misusing the class, but for the life of me I can't figure out where or why. I'm loading in an XML file and several images within the XML using , which seems to be working just fine:

 

function loadPhotoXML(photoID):void { 

 //Activate ImageLoader so that the XMLLoader can recognize them.
 LoaderMax.activate([imageLoader]);

// create loader + start loading
xmlLoader = new XMLLoader("../xml/hamilton.xml", {name:"xmlDoc", onInit:buildPortfolio});
	xmlLoader.load();

}

My goal is to use this to build a loading queue that immediately starts loading all the photos silently in the background where they'll be stored until I need to display them (you know... basically exactly what you created this for). When I need to add a specific photo to the display list I first check to see if the file I'm looking for is loaded:

 

// check to see if image is loaded before trying to add to stage
this.imageLoader = LoaderMax.getLoader("photo"+nextPortPhoto) as ImageLoader;

if (this.imageLoader.status == 2) {
showPhoto();
} else {
prioritizePhoto();
}

If the image is loaded I call showPhoto() where I add the image to the display list (below). If the image isn't loaded I call prioritizePhoto() (which prioritizes that photo in the queue, tracks the loading, and calls showPhoto() once it's fully downloaded).

 

function showPhoto():void { 

// retreive photo and add to stage
var subloadedImage:Bitmap = xmlLoader.getContent("photo1");
photo_HOLDER.addChild(subloadedImage);

}

function prioritizePhoto():void {
this.imageLoader = LoaderMax.getLoader("photo"+nextPortPhoto) as ImageLoader;
this.imageLoader.prioritize();
this.imageLoader.addEventListener(ProgressEvent.PROGRESS, photoProgressHandler, false, 0, true);
}

function photoProgressHandler(event:ProgressEvent):void {
if(event.bytesLoaded == event.bytesTotal){
	showPhoto();
}
}

Things seems to be getting fouled up in the showPhoto function. For some reason "xmlLoader.getContent("photo1");" keeps returning a value of undefined. The thing that is strange to me is that if I trace "xmlLoader.getLoader("photo1");" it returns "LoaderItem 'photo1' (url:../images/hamilton/food_1.jpg)."

 

Why would the getContent return undefined and getLoader return a value? I suspect it's because the content/photo I'm trying to get isn't actually loaded, but I don't know why that would be. I've poured through your documentation and examples, but for the life of me can't solve this. Sorry to bother you with what I'm almost certain isn't a bug report, but if you can shed any light on this for me I'd really appreciate it.

 

p.s. Despite my early stumbles, I can already tell this is going to be an amazingly useful tool. Kudos dude.

Link to comment
Share on other sites

I bet the problem is the fact that you're casting to a Bitmap instead of DisplayObject. Normally, an ImageLoader will return a Bitmap object but if a security error is thrown because the image is coming from another domain and there's no crossdomain.xml file in place, for example, LoaderMax will automatically adjust the LoaderContext and fall back to a more restricted mode, attempting to load the image again but this time it must return a Loader object instead of a Bitmap due to security restrictions in the Flash Player. Both Bitmap and Loader are DisplayObjects, so you can cast your variable to that to avoid issues. If you try casting a Loader as a Bitmap, Flash will null the variable.

 

Are you loading your image(s) from another domain?

 

Also, could you trace(LoaderMax.getContent("photo1")) and just see if it is returning content? If it's null, that means your object didn't load.

Link to comment
Share on other sites

Thanks for the quick reply.

 

I'm running all my files locally, so there isn't any cross domain loading happening. I tried to cast as a DisplayObject with no luck.

 

When I trace(LoaderMax.getContent("photo1")); it returns "undefined." Is there a flaw in the way I'm attempting to prioritize a specific image in the queue and add to the display list upon loading completion? (p.s.I realize the progress handler is functionally anemic right now, but as far as I understand things it should properly alert me once my image is loaded)

 

function prioritizePhoto():void {
this.imageLoader = LoaderMax.getLoader("photo"+nextPortPhoto) as ImageLoader;
this.imageLoader.prioritize();
this.imageLoader.addEventListener(ProgressEvent.PROGRESS, photoProgressHandler, false, 0, true);
}

function photoProgressHandler(event:ProgressEvent):void {	
if(event.bytesLoaded == event.bytesTotal){
	showPhoto();
}
}

Link to comment
Share on other sites

Ah yes, that is very likely the problem - you're not listening for the COMPLETE event. The progress event gets dispatched BEFORE the content is set internally, so you should definitely be waiting for the COMPLETE event. I bet that'll fix your problem, but please let us know.

Link to comment
Share on other sites

Perfection. Thanks Jack.

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.
×