MovieClip resizes, but its children’s height and width are not changed

actionscript-3flash-cs5

Changing the width and height of the parent MovieClip does not bring change in the width and height of the inner MovieClip. The parent MovieClip is placed at Stage and is resized manually. When I assign the dimension of the parent MovieClip to the inner MovieClip through code, the parent MovieClip dimension is changed. I want both MovieClip to be of same width and height at runtime. However, parent MovieClip dimension is changed at design time by me.

Example:

There are two MovieClip, one inside another. Now parent MovieClip is placed at Stage at design time and its dimension is (50,50) and the child MovieClip which is inside the parent MovieClip has also same dimensions (50,50). Now, I manually change the parent MovieClip dimension by pressing Q and stretching it with mouse, the dimension of the parent MovieClip is now (100,150) or whatever I like. Now double-click on parent MovieClip and check that inner MovieClip dimension remains same i.e. (50,50)
Now in AS3 code, I change the width and height of inner MovieClip like this:

saveheight = parentmc.height;
savewidth  = parentmc.width;

now I change the child MovieClip according to the dimensions of the parent MovieClip like this:

parentmc.inner_mc.width = parentmc.width;
parentmc.inner_mc.height = parentmc.height;

but this brings change in parentmc also so I reassign value to parentmc like this:

parentmc.height = saveheight;
parentmc.width = savewidth;

In above case, parentmc and inne_rmc dimension should be same i.e (100 ,150). With swapping the values as above, I get parentmc and inner_mc to be of same dimension, but object size is never (100, 150), I have checked it with pixel-perfect air app.

Best Answer

In your code, you are neglecting to account for the transformation to the parent that you did with the 'Q' tool in the authoring environment. The childmc's width and height are expressed in terms of parentmc's transformed coordinate space. If you wish to scale the inner clip to a specific size in stage coordinate space you need to account for the scale of the parent that resulted from your transform:

   parentmc.inner_mc.width = parentmc.width/parentmc.scaleX;
   parentmc.inner_mc.height= parentmc.height/parentmc.scaleY;

Also, if the clips aren't aligned (e.g. registered by their upper left corner and with the child at 0,0), enlarging the child could push out the boundaries of the parent.

You can also use the parent's transform matrix if you prefer that over using scaleX and scaleY.

UPDATE 4.8.11: Were you perhaps asking to do this (runtime removal of the authoring-time transform)?

saveheight = parentmc.height;
savewidth  = parentmc.width;

parentmc.scaleX = 1.0;  // Undo authoring-time scale transform
parentmc.scaleY = 1.0;  // Undo authoring-time scale transform

parentmc.inner_mc.width = savewidth;
parentmc.inner_mc.height = saveheight;

parentmc.width = savewidth;
parentmc.height = saveheight;

Note: I'm not at a computer set up with Flash to test this, so please leave me a comment if this does not do what you are expecting, and I will happily check my work and follow up.

Related Topic