Groups | Blog | Home
all groups > flash actionscript > december 2005 >

flash actionscript : scaling in proportion please...


krustafarian
12/13/2005 11:04:32 PM
can anyone help me adapt the following script to allow my background image
(bg1) to scale proportionately whilst filling the stage, at the moment it fills
the stage but stretches unproportionately.

startingSizeX = Stage.width;
startingSizeY = Stage.height;
Stage.scaleMode = "noScale";
bg1._width = Stage.width;
bg1._height = Stage.height;

listener = new Object();
listener.onResize = function ()
{
bg1._width = Stage.width;
bg1._height = Stage.height;
};
Stage.addListener(listener);


cheers
NSurveyor
12/13/2005 11:14:38 PM
THis is how I would do it:

1. Find out which dimension of the bg is larger (this is the one we want to
scale to exactly the dimension of the stage it corresponds to. For example, if
the bg was 10 x 15 and the stage is 30 x 40, we want to make the height of the
bg the same as the stage).

2. Then, adjust the other dimension accordingly using the _xscale and _yscale
properties. READ about these properties in the Help Panel, they are KEY. (ex:
_xscale = _yscale or _yscale = _xscale)

How to implement that in actionscript:

Stage.scaleMode = "noScale";
listener = new Object();
listener.onResize = function ()
{
if(bg1._width>=bg1._height){
bg1._width = Stage.width;
bg1._yscale = bg1._xscale;
}else{
bg1._height = Stage.height;
bg1._xscale = bg1._yscale;
}
};
listener.onResize();//initial adjustment
Stage.addListener(listener);


krustafarian
12/13/2005 11:26:35 PM
NSurveyor
12/13/2005 11:47:29 PM
Sorry, I assumed you wanted the whole background clip to fit on the stage. But
now, I realize it's obvious that you want it to take up the entire stage
without distortion. So all you need to do, is make sure that if the HEIGHT is
greater than the WIDTH, then the width should be set to the stages width and
the height in proportion to the width (and vice versa for WIDTH greater than
HEIGHT). To fix the code, change:

if(bg1._width>=bg1._height){ //btw >= isn't neccessary because of the else, it
can take care of the ==, > works fine

with

if(bg1._height>bg1._width){
krustafarian
12/13/2005 11:57:24 PM
thanks v much for your help dude, i've been playing about and this seems to
work for me: (i need to set TL align otherwise i get weird margins to the top
and left (no idea why!)

Stage.scaleMode = "noScale";
Stage.align = "TL"
listener = new Object();
listener.onResize = function ()
{
backdrop._width = Stage.width;
backdrop._height = Stage.height;
if(backdrop._xscale>backdrop._yscale){
backdrop._yscale = backdrop._xscale;
}else{
backdrop._xscale = backdrop._yscale;
}
};
listener.onResize();
Stage.addListener(listener);


NSurveyor
12/14/2005 12:02:55 AM
NSurveyor
12/14/2005 12:06:17 AM
Oh yeah, and you need the TL because otherwise the content would stay centered.
So basically, you're flash would resize from the center keeping all the content
there, which would make a [stage color]ed border around the content. And when
you resize the background, it will probably cover up the lower and right
border, but the 0,0 point would be where the flash content starts, so it would
leave the upper and left border.
krustafarian
12/14/2005 12:08:49 AM
NSurveyor
12/14/2005 1:07:14 AM
AddThis Social Bookmark Button