Jump to content

How do world bounds get calculated?


snowbillr
 Share

Recommended Posts

Hi everyone!

I'm playing around with positioning objects using the alignIn method, and am getting confused about how the bounds of the `world` object are being calculated.

Take a look at the codepen here: 

Aligning the text according to `world.bounds`, `world.getBounds()`, and `world.getLocalBounds()` all ends up with different results. Could somebody help me understand why these calls all return different bounds?

(Interesting to note that you can see this happening with the `stage` object too between its `getBounds()` and `getLocalBounds()` methods)

EDIT: This is all happening in the `create` method of the State. Not sure if that has anything to do with it either.

Edited by breed
Link to comment
Share on other sites

All groups have a getBounds() method. It makes a rectangle that covers every visible child of the group, i.e., reaching the leftmost, topmost, rightmost, and bottommost children, wherever they are. It's mostly useful for rendering.

world.bounds is a boundary like fence rather than extent. It's a fixed rectangle equal to the game size (if unmodified) or larger (if you've set it yourself).

For alignment you probably want world.bounds.

Link to comment
Share on other sites

Ah, I see! So if I call 'getBounds()' on World it will give me back a rectangle covering all its children. But accessing the 'bounds' property of World gives me its "fence" as you called it. 

Good explanation, thank you!

And A+ codepen example!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...