Hide
Meet people who work on similar things as you – get help if you need it Join Siafoo Now or Learn More

Quick and Memorable Proportional Resizing in Flash Atom Feed 0

When needing to scale loaded assets in ActionScript so that no side is larger than a particular dimension, you can spare manual proportion calculation by using the scaleX and scaleY display object properties.

When needing to scale loaded assets in ActionScript so that no side is larger than a particular dimension, you can spare manual proportion calculation by using the scaleX and scaleY display object properties.

Manual proportional scaling code looks like this:

myDisplayObject.height = (originalHeight * newWidth) / originalWidth;
myDisplayObject.width = newWidth;

That's fine and dandy, but I typically need to write out the proportion math out in my head or on paper. Utilizing the scaleX and scaleY display object properties, we can accomplish proportional scaling in more memorable manner. When you change a display object's width or height, the scaleX and scaleY properties automatically get updated. To keep the display object proportional, set the smaller side's scale to the scale as the larger side after the larger side's dimension is set.

var maxSize:Number = 80;
if (myDisplayObject.width > myDisplayObject.height) {   // horizontal asset
        myDisplayObject.width = maxSize;
        myDisplayObject.scaleY = myDisplayObject.scaleX;
} else {        // vertical asset
        myDisplayObject.height = maxSize;
        myDisplayObject.scaleX = myDisplayObject.scaleY;
}