AS3 – Unable to resize Image width and height

I have a problem that when I load image it doesn’t resize to the value I set in my button, so the button resize itself to fill the image, i think!

And when I see value like 200,200 the resulted image is like 3-4 times bigger than 100,100

public function MyButton()
{// this is ctor
   ImageLoader = new Loader();
   addChild(ImageContainer);
}
public function SetWidthAndHeight(newWidth: Number, newHeight: Number): void
{
    width = newWidth;
    height = newHeight;
    //ImageContainer is a MovieClip inside the button
    ImageContainer.height = newHeight;
    ImageContainer.width = newWidth;
}
public function LoadImage(address: String): void
{    
   
    ImageLoader.load(new URLRequest(address));
}

I set the value like

 var g_newButton = new MyButton();
 g_newButton.ImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnCompleteHandler);**strong text**

 g_newButton.SetWidthAndHeight(100,100);
 g_newButton.LoadImage("myImage.jpg");
function OnCompleteHandler(loadEvent: Event) 
{   
     //g_newButton.ImageLoader.content is always null
    trace("button width " +g_newButton.width);
    trace("button height " +g_newButton.height);
    g_newButton.ImageContainer.addChild(loadEvent.currentTarget.content);
    addChild(g_newButton);
}

when I run, these are the values I get

button width 750

button height 385.55

so Why i am not getting

button width 100

button height 100
what should i do to make the image resize to fill a button or to have the same size of the button

  • Because when you set the ImageContainer‘s width and height, it ALSO affects its parent (which is the MyButton instance) width and height, which is AFTER you set them to 100, 100.

    – 

  • the problem was with loading the image and it’s fixed, but I wonder as part of my fix, I removed addChild(ImageContainer), so are you saying that setting a child dimensions propagate to it’s parent? so I only need to set the dimensions to the most inner child? this will set the parent size will the same values??

    – 

Leave a Comment