PDA

View Full Version : setting the width of dynamically loaded images



fideldonson
October 31st, 2008, 06:16 AM
In a project im working on i get the following strange behaviour:
I load an image with a loader object
listen for the contentLoaderInfo Event.COMPLETE
and then set the width of the target.content
problem is that the width of the loaded image is not set.

can the COMPLETE event fire before the image is completed or what could be the problem?

here's the code:

var shopImageLoader:Loader=new Loader();
shop.shopItem.addChild(shopImageLoader);

shopImageLoader.load(new URLRequest("cover_url")));
shopImageLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, onShopImageComplete);

function onShopImageComplete(evt:Event):void{
evt.target.content.width=68;
evt.target.content.height=68;
}

Pier25
October 31st, 2008, 07:06 AM
use Event.INIT instead

Magik5
October 31st, 2008, 07:31 AM
youve never set the width/height of the actual loader, youve basically added an empty loader to the stage(default w/h is 0,0) so you need to call shopImageLoader.width=68; etc in the onShopImageComplete handler

theRemix
November 2nd, 2008, 01:19 AM
you may use Event.COMPLETE

but instead of


function onShopImageComplete(evt:Event):void{
evt.target.content.width=68;
evt.target.content.height=68;
}

use either

function onShopImageComplete(evt:Event):void{
evt.target.width=68;
evt.target.height=68;
}

or

function onShopImageComplete(evt:Event):void{
shopImageLoader.width=68;
shopImageLoader.height=68;
}

should work.