I'm using a Stage of LibGDX to draw a text, in my case the FPS. But after resizing, the position of the text does not behave properly.

As you can see in the attached gif:

enter image description here

The resizing function does work correctly. Also here is my code of the UI-renderer:

GameScreen screen;
BitmapFont font;

FreeTypeFontGenerator generator;
FreeTypeFontGenerator.FreeTypeFontParameter parameter;

private Texture map;

public UIStage(GameScreen screen) {
    this.screen = screen;
    generator = new FreeTypeFontGenerator(Gdx.files.internal("font/acknowtt.ttf"));
    parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
    parameter.size = 24;
    parameter.borderWidth = 2;
    parameter.borderColor = new Color(0, 0, 0, 1F);
    parameter.borderStraight = true;
    font = generator.generateFont(parameter);
    this.map = new Texture(Gdx.files.internal("levels/map/" + 
        screen.map.getLevel().getName() + ".png"));

public void draw() {
    this.getBatch().setColor(1F, 1F, 1F, 1F);

        "FPS: " + GameScreen.fpsLogger.getFPS(), 
        this.getViewport().getScreenHeight() - 44);

How can I make my text behave properly?


Make sure you resize the viewport when you resize the window.

You should be able to override the function 'resize' in your ApplicationAdapter.

public class GameClass extends ApplicationAdapter {

    public void resize(int width, int height) {
        YourUIStage.getViewport.setScreenSize(width, height);

| improve this answer | |
  • \$\begingroup\$ It's still not working... Is something wrong with my render? hastebin.com/iramulidaq.avrasm (I wanted to post the code here, but can't figure out how...) \$\endgroup\$ – okaay Aug 2 '15 at 12:24
  • \$\begingroup\$ I'm afraid i can't help you if I can't see more of the code. \$\endgroup\$ – Jonas Aug 2 '15 at 15:17
  • \$\begingroup\$ What else do you need? I thought that was everything necessary... \$\endgroup\$ – okaay Aug 2 '15 at 15:41
  • \$\begingroup\$ Try to do cam.setToOrtho(false, width, height); on your camera whenever you resize the screen. \$\endgroup\$ – Jonas Aug 6 '15 at 11:12
  • \$\begingroup\$ or something like YourUIStage.getViewport.update(width, height, true); \$\endgroup\$ – Jonas Aug 8 '15 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.