Jump to content
Search Community

XMLLoader with dynamic ImageLoader - no width/height

spiralstatic test
Moderator Tag

Recommended Posts

Hi there,

 

I'm using XMLLoader to load my XML, then load an an image that sits inside the XML.

 

The image loads fine and I can use this code to show it -

var photo:ContentDisplay = LoaderMax.getContent("bg");
addChild(photo);
TweenLite.from(photo, 1, {alpha:0});

 

The problem is if I want to use LiquidStage to display the image (by adding it to a liquid area). If I do this then the image doesnt't appear until you manually resize the browser. This happens even if I call update() on the liquidStage or liquidArea objects.

 

I traced out the width/height of the image once it's been added to the stage, and they are both zero -

var photo:ContentDisplay = LoaderMax.getContent("bg");
addChild(photo);
TweenLite.from(photo, 1, {alpha:0});
trace(photo.width, photo.height); // 0 0

 

This is obviously an issue - but I don't understand why it's happening or how I can fix it. I don't want to add the display object before it's loaded, so don't want to hard code a width/height anywhere in the load constructor. Also I'm dynamically creating the imageLoader so have less control on how it loads the image (I think!?).

 

Any help would be much appreciated!

 

Thanks,

 

Matt

Link to comment
Share on other sites

Are you running that code AFTER the image has fully loaded (after the ImageLoader's COMPLETE event fires)? If width/height are 0, then it sounds like you're probably calling it BEFORE the image loads. Two things I'll mention:

 

1) You can set a width/height in the ImageLoader vars parameter itself which will immediately make the ContentDisplay object that size (drawing an invisible rectangle inside it). That means you could attach() it to the LiquidArea before the image loads and it'd work great.

 

2) If you'd rather not set the width/height on the ImageLoader initially, that's fine, but make sure you either attach() it to the LiquidArea AFTER it fully loads OR call the update() method of the LiquidArea after it loads. Either way, the point is that LiquidArea can't scale it properly until it can figure out the size of it which it can't do until it's fully loaded.

Link to comment
Share on other sites

Hey, thanks for the quick response.

 

I'm taking a look at this right now but can't seem to get anywhere.

 

The XMLLoader has been appended to a LoaderMax loader, and I'm trying to extract the width/height of the image when the LoaderMax onComplete handler has fired (i.e. AFTER everything should have loaded, right?).

 

If I add an onChildComplete handler to the XMLLoader no event appears to be dispatched for the subloads (the image in the XML). How do I access the ImageLoader seeing as it was created dynamically by the XMLLoader?

 

Here's the code - I've removed any liquidStage stuff as it's irrelevant -

 

private function initLoad():void
	{
		queue = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
		queue.append( new XMLLoader(rootURL + XMLFile, {name:"xmlDoc", onComplete:completeXMLHandler, onChildComplete:onChildLoaded}) );

		queue.load();
	}

	private function completeXMLHandler(event:LoaderEvent):void 
	{
		trace("XML loaded");
		queue.replaceURLText("{imageDirectory}", rootURL + GlobalAccess.pathImages, true);
		queue.getLoader("xmlDoc").getLoader("bg").load();
	}

	private function onChildLoaded(event:LoaderEvent):void
	{
		trace("sub loaded " + event.target);
	}

	private function progressHandler(event:LoaderEvent):void 
	{
		trace("progress: " + event.target.progress);
	}

	private function completeHandler(event:LoaderEvent):void 
	{
		trace(event.target + " is complete! (everything loaded)");

		var photo:ContentDisplay = LoaderMax.getContent("bg");
		addChild(photo);
		TweenLite.from(photo, 1, {alpha:0});
		trace(photo.width, photo.height);

		GlobalAccess.contentXML = LoaderMax.getContent("xmlDoc");
		trace("Here's the XML... \n" + GlobalAccess.contentXML);
	}

 

Hope you can help,

 

Thanks

Link to comment
Share on other sites

Ah yes, you set it up in a way that could cause completeHandler() to get called BEFORE the background image finished loading. You set the LoaderMax's onComplete to call completeHandler() and inside your XML, it looks like you do NOT set the node's "load" attribute to "true" so it doesn't force it to load and integrate its progress into the XMLLoader's progress. In other words, LoaderMax finishes loading its contents (XMLLoader) quickly because it only has to load the XML text itself, not the nested loader(s) inside the XML. Therefore it correctly calls completeHandler() before the image begins loading.

 

You could either set load="true" on your XML tag(s) or you could remove the onComplete from the LoaderMax and instead add it to your "bg" ImageLoader. Like inside your completeXMLHandler() method, put LoaderMax.getLoader("bg").addEventListener(LoaderEvent.COMPLETE, completeHandler);

 

Make sense?

Link to comment
Share on other sites

Oh yeah, you're right - I removed the "load" attribute from the XML nodes so that I could perform the replaceURLText.

 

Your solution worked but I decided to write it a neater way and instead append the "bg" loader to the original "queue" loader (so now my mainQueue complete event only fires once everything has completely loaded) -

 

private function initLoad():void
	{
		queue = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
		queue.append( new XMLLoader(rootURL + XMLFile, {name:"xmlDoc", onComplete:completeXMLHandler}) );

		queue.load();
	}

	private function completeXMLHandler(event:LoaderEvent):void 
	{
		trace("XML loaded");
		queue.replaceURLText("{imageDirectory}", rootURL + GlobalAccess.pathImages, true);
		queue.append(queue.getLoader("bg"));
	}

 

Thanks for your help on this one - much appreciated!

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