PDA

View Full Version : container MC scaling problem


pixum
07-19-2008, 10:18 PM
hi,

i guess a real beginner question :eek: -

i am working on an image gallery, in which the thumbnails will float up and down randomly, with the stageHeight as border.
This works all fine, but now I wanted to add it to a container, so I can change it to a new size (making it bounce not on the entire stageHeight).

Pls see code below, just a simple example without the floating function and only two testboxes, but enough to show the problem:

var container:MovieClip = new MovieClip();
addChild(container);

/*including the following lines doesn't display anything anymore - leaving it out works fine:
container.height = 400;
container.width = 400;*/

var firstBox:TestBox = new TestBox();
var secBox:TestBox = new TestBox();

container.addChild(firstBox);
container.addChild(secBox);

firstBox.x = 50;
secBox.x = 350;


Leaving the 2 lines in question out sizes the container to the whole stage again ...

pixum

CW.Allen-Poole
07-20-2008, 08:07 AM
var container:MovieClip = new MovieClip();
addChild(container);

/*including the following lines doesn't display anything anymore - leaving it out works fine:
container.height = 400;
container.width = 400;*/

container.addChild(firstBox);
container.addChild(secBox);


Because container does not have any content, you can't scale it (you'll note that if you put the two lines in question after it has scalable content, it will continue to display). You will either need to add everything to it and then scale or create an object at the maximum x, y of the container. If you do decide that you would like to scale after adding all of the objects, you may get distortions which will have to be handled. Fortunately, that is fairly easy with the Matrix class:

function undistort(displayObject:DisplayObject){
// This is the sum of all transformations which have been applied to the object.
var mat:Matrix = displayObject.transform.concatenatedMatrix;

// invert the matrix to get to the opposite of that transformation.
mat.invert();

// The current matrix without the parent's transformations.
var finalMat:Matrix = displayObject.transform.matrix;

// Multiply the inverse
finalMat.concat(mat);

// apply the result.
displayObject.transform.matrix = finalMat
}

// Then apply the above after the bounds are defined and before position
// otherwise the above will remove the x and y you've set.
container.addChild(firstBox);
container.addChild(secBox);

container.height = 400;
container.width = 400;

undistort(firstBox);
undistort(secBox);

firstBox.x = 50;
secBox.x = 350;


It may seem a bit daunting at first, but it boils down to this: Flash represents scaling and distortion as a series of fractions. If you multiply by the inverse (1/2 * 2/1), just like in fifth grade math, you will end up with 1 -- no distortion.

pixum
07-20-2008, 10:13 AM
hi poole, thanks alot for this and the additional info of the matrix! now that i read it, it seems totally clear that you cannot scale nothing 'duh' ... i am still running quite often into this hierarchy traps ...