Search code examples
actionscript-3sizespritescale

Set sprite width or height without scaling it's children


I am trying to dynamically change the width and height of a Sprite object, which is a container for other similar Sprite objects. The container object automatically changes it's size according to the size of it's children, but when I change the position of the children objects, the size of the container stays the same and it's children appear to be placed outside of the container.

I tried to solve this problem by using something like this:

if (container.width < (child.x + child.width))
{
    container.width = (child.x + child.width);
}

But when I use this code, the container object's children are scaled.
Is there a way to change the container's size without scaling it's children?


Solution

  • That's not possible directly, the size of a sprite depends on the bounds of its contents. So the only option would be to resize a child of the sprite, or place a dummy child on x: 0, y: 0 (assuming this would be your desired anchor point).

    The coordinates of the contents bounds is not taken into account as you might expect, when having for example one child that is 100 x 100px, placed on x: 50, y: 50, the parents size would still be 100 x 100px, not 150 x 150px.

    Adding another child of 100 x 100px on x: 0, y: 0 would increase the bounds to 150 x 150, because it now starts on child one x: 0, y: 0 and ends on child two x: 50, y: 50 + 100 x 100px.

    content bounds