Skip to main content
June 10, 2009
Question

AS3 - Resizing a MovieClip according to the size of a dynamically loaded image

  • June 10, 2009
  • 2 replies
  • 2378 views

Hello all,

I wonder if any of you can point me in the right direction. In my stage I have a movieclip where I want to load several images with different sizes. This mc called "container" adds the loader to the stage so I'm asking for the loader width and height once has finished loading and then passing these to the width and height of the mc container so it resizes accordingly. But...it doesn't work.
Here is my code:

var pic:Loader;
var totalImages:int = 10;

for (var i:uint = 0; i<totalImages; i++){
    
    pic = new Loader()
    container.addChild(pic)
   pic.load(new URLRequest(myImages))
    pic.addEventListener(Event.COMPLETE, resizeContainer)
}

function resizeContainer(e:Event):void{

    container.width = pic.width;
   container.height = pic.height


}

The above code works but doesn't resize properly and it actually resizes the container and the photo at the same time...What am I doing wrong?

Thanks a lot for your help in advance.

Txusm

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
June 10, 2009

use:


var pic:Loader;
var totalImages:int = 10;
var border:uint = 10;
var prevX:Number = 0;
var prevW:Number = 0;


var i:uint=0;
var container:Container;  // create a class for your container moviecip
loadF();

function loadF(){
    container = new Container();
    addChild(container);


    pic = new Loader()
   
    pic.load(new URLRequest(myImages))
    pic.addEventListener(Event.COMPLETE, resizeContainer)
}

function resizeContainer(e:Event):void{
    container.x = prevX+prevW;

    container.width = pic.width+border;
    container.height = pic.height+border;
    container.addChild(pic);
    pic.x=pic.y=border/2;

    prevW=container.width;
    prevX=container.x;

    i++;
    if(i<totalImages){
        loadF();
    }


}


kglad
Community Expert
Community Expert
June 10, 2009

you're adding your loader to container before loading is complete.  then when loading is complete container is already resized because its child (pic) is resized.

to remedy, don't add pic to container until loading is complete and after you're resize container.

June 10, 2009

Hi kglad, thanks for your answer but I think it has more to do with the fact that what I'm trying to resize is a graphic inside the mc "container" when it should be also a mc. So, when I put another mc inside "container" this works:

container.bg.width = e.currentTarget.content.width;

container.bg.height= e.currentTarget.content.height;

Tx