views:

99

answers:

1

I am trying to center an image in the middle of the stage and scale it proportionally to the correct aspect ratio based on its loader's size when the image is loading.

In my main app runner I do:

private var rec:Rectangle = new Rectangle();
private var img:BitmapDisplay = new BitmapDisplay();
stage.addEventListener(Event.RESIZE, setRect);
img.imageURL = "some/path/to/image.jpg";
addChild(img);

private function setRect():void
{
    var horz:Number = stage.stageWidth / 16;
    var vert:Number = stage.y + stage.stageHeight / 16;

    rec.x = horz;
    rec.y = 80;
    rec.width = stage.stageWidth - horz;
    rec.height = stage.stageHeight - (vert * 2) - rec.y;

    img.boundary = rec;
}

Then in my image (BitmapDisplay) class I have:

// sets the boundary of the image and resizes it
public function set boundary(rect:Rectangle):void 
{
    _imageBoundary = rect;
    resizeImage();
}

private function resizeImage():void 
{
    var aspect:Number = _ldr.content.width / _ldr.content.height;   
    var cAspect:Number = _imageBoundary.width / _imageBoundary.height;

    if (aspect <= cAspect) 
    {
        _ldr.height = _imageBoundary.height;
        _ldr.width = aspect * _ldr.height;
    }
    else 
    {
        _ldr.width = _imageBoundary.width;
        _ldr.height = _ldr.width / aspect;
    }

    var _pad:int = 7;

    _ldr.x = (_imageBoundary.width - _ldr.width) / 2 + _imageBoundary.x - _pad;
    _ldr.y = (_imageBoundary.height - _ldr.height) / 2 + _imageBoundary.y - _pad;
}
}   

Is there something obvious keeping this from working right? I want a 16th of the stage to be the bounds for the image and for the image to scale/resize based on this rect.

Thanks...

A: 

It turns out that this had more to do with the loader not being ready to be resized. There was some error checking in my code that needed to be reformatted, so now when the resize function gets called, it doesn't do anything until the loader is ready and the boundary is set.

jml