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?
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 onx: 50, y: 50, the parents size would still be100 x 100px, not150 x 150px.Adding another child of
100 x 100pxonx: 0, y: 0would increase the bounds to150 x 150, because it now starts on child onex: 0, y: 0and ends on child twox: 50, y: 50+100 x 100px.