Use setDisplaySize on a Container

Hi everyone,

I’m slowly learning Phaser 3 and it’s a lot of fun. I’m currently practicing with Containers and I’ve encountered an issue when using the setDisplaySize method. What I understand is that this method is supposed to set the scale property according to the real size of the object (scale = desiredSize / actualSize). Am I getting this right ?

I was trying to apply it to a Container so my guess is that it should get the Container size first depending on its children and then calculate the appropriate scale factor. I’ve read here and there that the size of a Container is not calculated often as it can be expensive in processing power if the Container has a lot of children. The method getBounds does that well so I’ve been using it to manually calculate the scale factor and it gives good results.

When using the method setDisplaySize directly on the container, it is not displayed anymore and I get NaN for width and height when calling getBounds.

You can have a look at the example

What is the explanation for this behavior ? Am I missing something here ?

Thank you

You need to call setSize() before setDisplaySize().

A container’s intrinsic size is 0, so that’s where the NaN is coming from.

You can assign an intrinsic size from the bounds: https://codesandbox.io/s/sad-galileo-6b6kb

Thank you for the quick answer, it helped me understand containers a lot better. Maybe what you wrote should be somewhat added to the documentation, if its not already there.

It also helped me identify what I think is a bug in the way containers bounds are created.