HeroicNate
HeroicNate

Reputation: 35

Flash AS3: How do you calculate the width of an image loaded into a movieclip?

I am loading an image into a movie clip via AS3, and what I want to do is center the image inside the movieclip. The problem is that I can't seem to grab the width value of the image being loaded. Below is what I'm doing:

imageLoader = new Loader();
imageLoader.load(new URLRequest(event.target.name));
screenBox.screenHolder.addChild(imageLoader);
trace(this.imageLoader.width);

When I trace the width, it always comes back at zero(0) even though there is an image inside the imageLoader. What is it I need to do to find the actual width so I can center the image?

Upvotes: 1

Views: 1835

Answers (2)

Dan Heberden
Dan Heberden

Reputation: 11068

ActionScript Retrieving Width/Height Of Loader Image

you have to load it first

imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedImage);
imageLoader.load(new URLRequest(event.target.name));
screenBox.screenHolder.addChild(imageLoader);

function loadedImage(event:Event):void {
   trace(event.target.content.width);
}

Upvotes: 1

Robusto
Robusto

Reputation: 31883

Have you tried doing validateNow() after adding the imageLoader and before you try to get its width?

Upvotes: 0

Related Questions