LibGDX text not rendering properly on larger screens

给你一囗甜甜゛ 提交于 2020-01-06 15:50:14

问题


On my 854 x 480 LG Leon it renders fine, but on a larger screen resolution (2560 x 1440) this happens: http://imgur.com/a/rcbJK.

The code for processing the text is: http://imgur.com/a/c316e.

I've tried expanding the bounds of the Label because I thought that it might be constricting the text, but it still won't display properly.

What could be causing this?


回答1:


The different mobile phone has the different aspect ratio in their screen size . that is the reason it shows different different screen. to avoid this problem you must use the viewPort. So you will be able to handle the different screen size. for more details, you must read the libgdx documentation. here I'm posting a sample code which can help you to handle the different screen size. there are three kinds of viewports mainly using 1) fillviewport 2) fitviewPort 3)stretchviewport here is the documentation in detail.

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/viewport/FillViewport.html

public class myGame extends ApplicationAdapter {

public OrthographicCamera camera;
public Viewport viewPort;
private SpriteBatch batch;

public myGame() {

}

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camera = new OrthographicCamera();
    camera.position.set(0, 0, 0);
    camera.update();
    camera.setToOrtho(false, 1280, 800);
    viewPort = new FillViewport(1280, 800, camera);


}

@Override
public void dispose() {
    batch.dispose();
}





@Override
public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
    float deltaTime = Gdx.graphics.getDeltaTime();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(sprie,0,0)
    batch.end();

}

@Override
public void resize(int width, int height) {
    viewPort.update(width, height);
}

@Override
public void pause() {
}

@Override
public void resume() {
}
}
// don't copy paste it . just try to understand and implement it.



回答2:


You should use Scene2D and ViewPort.

Also , be careful to use coordinates (i.e : x: 64 , y:32 changes in every different screen size)




回答3:


This is the common issue when your running your game in multiple devices. Because your game is running in different aspect ratios in different screens . it's generally called the multi screen issue. To solve this problem the libgdx is providing it's on class called the viewPort. Mainly you can see three viewpors. 1)Fill viewPort. 2)Fit viewPort. 3)stretch viewport. For more details you can go through the libgdx documentation. Here im posting some example code which can solve your multi screen issues.

public class myGame extends ApplicationAdapter {

public OrthographicCamera camera;
public Viewport viewPort;
private SpriteBatch batch;
private BitmapFont myScoreFont;

//General screen resolution public int APP_WIDTH=1280; public int APP_HEIGHT=800; public int fontPositionIn_X=600; public int fontPositionIn_Y=400; public myGame() {

}

@Override
public void create() {
        myScoreFont = new    BitmapFont(Gdx.files.internal(Constants.PATH_TO_MY_SCORE_FONT), true);
    batch = new SpriteBatch();
        float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
        camera = new OrthographicCamera();
    camera.position.set(0, 0, 0);
    camera.update();
    camera.setToOrtho(false, APP_WIDTH, APP_HEIGHT);
    viewPort = new fillViewPort(1280, 800, camera);


}

@Override
public void dispose() {
    batch.dispose();
}





@Override
public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
    float deltaTime = Gdx.graphics.getDeltaTime();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    myScoreFont.draw(batch,"any texts", fontPositionIn_X, fontPositionIn_Y)
    batch.end();

}

@Override
public void resize(int width, int height) {
    viewPort.update(width, height);
}

@Override
public void pause() {
}

@Override
public void resume() {
}

}

so by using the viewPort you can be able to play your game in all the different screens.



来源:https://stackoverflow.com/questions/39810169/libgdx-text-not-rendering-properly-on-larger-screens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!