Question

I'm making a game in libGDX. In my menu screens I used to have a stretch viewport for the stage but now I want to maintain the aspect ratio so I switched to a fill viewport with a fixed height.

So the problem I am having is with the buttons that used to be at the edge of the screen. For example I had a button in one screen with an x position of 0 and now (depending on device) it can end up outside the viewport bounds. So I was wondering how I solve this using a fill viewport or similar. Is there anyone who has a similar issue and how did you solve it?

Thank you

Was it helpful?

Solution

viewport.getLeftGutterWidth()

This will return the width of the left "black bar". A black bar appears, when the viewport is smaller than the actual window. In your case, this can never happen, because it's a FillViewport.

Depending on your aspect ratio, viewport.getLeftGutterWidth() will return either 0, in case the viewport got over-extended on the y-axis, or it has a negative value in case it over-extended on the x-axis.

So what you are looking for is Math.abs(viewport.getLeftGutterWidth()) for the x-value of the left-most visible coordinate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top