Reputation: 887
I'm racking my brain trying to figure out what's going on here... I'm using a FitViewport which is working great whenever I resize the screen, but when it's first created during the constructor it doesn't fit itself into the screen like it's supposed to (and like it does when I resize).
Here's the code for my constructor and for the resize() method:
public class UpgradesScreen implements Screen {
private ScreenHandler sh;
private OrthographicCamera cam;
private Viewport viewport;
public UpgradesScreen(ScreenHandler sh) {
Gdx.app.log("UpgradesScreen", "Attached");
this.sh = sh;
cam = new OrthographicCamera();
cam.setToOrtho(true, sh.V_WIDTH, sh.V_HEIGHT);
viewport = new FitViewport(sh.V_WIDTH, sh.V_HEIGHT, cam);
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
@Override
public void resize(int width, int height) {
Gdx.app.log("UpgradesScreen", "resizing");
viewport.update((int) width, (int) height);
}
I've checked to make sure that Gdx.graphics.getWidth() gives the same figure as the 'width' argument the resize() method receives when I resize the screen (likewise with the 'height' values).
As far as I can tell the viewport should behave exactly the same during the constructor as it does when I resize. So why is it being stubborn and not updating to fit?
Here's what it looks like after I resize:
...and here's what it looks like when the screen is first created, before the window gets manually resized:
Upvotes: 1
Views: 853
Reputation: 7057
You are creating a new Viewport
object which is not being used at all at start.
Call
viewport.apply();
Before your draw calls.
Also, resize
method is called at start by default. Use it to initialize game objects in stead of constructor.
Edit:
Have a look at LibGDX Lifecycle.
resize
is always called before render
. There should not be different behavior at start and afterwards. That is your root problem. Try fixing it first.
Upvotes: 1