【问题标题】:LIBGDX font different positions on different platformLIBGDX字体在不同平台上的不同位置
【发布时间】:2017-06-29 09:51:45
【问题描述】:

当我运行我的应用程序的 android 版本时,使用 Bitmap 绘制的文本在我的桌面版本中显示的位置(方式)与我的应用程序的 android 版本中不同。我已经查看了这个问题并读到我必须将 Projectmatrix 设置为组合,但这并没有帮助。不知道它跨平台在不同位置显示文本的原因是什么。

public class HighScoresScreen implements Screen{

    private CrossplatformApp game;
    private Stage stage;
    private TextButton backButton;
    private OrthographicCamera camera;
    private Table table;
    BitmapFont font = new BitmapFont();
    private Skin skin;
    private Texture background;
    private String[] data;
    private String[] playerscores;
    String nm = "";
    String sc = "";

    public HighScoresScreen (CrossplatformApp game) {
        this.game = game;
        this.camera = new OrthographicCamera(Constants.WIDTH, Constants.HEIGHT);
        this.camera.position.set(Constants.WIDTH/2, Constants.HEIGHT/2, 0);
        this.camera.update();
        game.batch.setProjectionMatrix(this.camera.combined);
        this.skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
        this.background = new Texture("Screens/HighscoresScreen/highscores.png");
    }


    @Override
    public void show() {

        backButton = new TextButton("back", skin);
        backButton.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                game.setScreen(new MenuScreen(game));
                MenuScreen.musicHandler.stopMusic();
            }
        });

        Gdx.input.setInputProcessor(stage);

        JSONfunctions json = new JSONfunctions();
        parseJSON parse = new parseJSON(json.doInBackground());

        for (String i : parse.getNames()){
            if(i != null) {
                nm += i;
                nm += "\n\n";
            } else {
                break;
            }
        }

        System.out.println(nm);
//
        for (String i : parse.getScores()){
            if(i != null) {
                sc += i;
                sc += "\n\n";
            } else {
                break;
            }
        }

        table = new Table();
        table.setFillParent(true);
        table.bottom();
        table.add(backButton).size(100, 100);
        stage.addActor(table);
    }

    @Override
    public void render(float delta) {
        game.batch.begin();
        game.batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        font.draw(game.batch, nm, 100, 300);
        font.draw(game.batch, sc, 480, 300);

        game.batch.end();

    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();

    }
}

【问题讨论】:

  • 您尝试过使用视口吗?
  • @Expiredmind 是的,我有,像这样:this.camera = new OrthographicCamera(); this.stage = new Stage(new FillViewport(Constants.WIDTH, Constants.HEIGHT, camera)); 然后在渲染方法中绘制这个阶段
  • 我认为你可以使用Label,然后将其添加到舞台中。然后你可以删除font.draw。舞台中的viewport可以很好的处理不同的分辨率。

标签: java android libgdx


【解决方案1】:

您需要更新相机的视口,然后使用更新的视口更新SpriteBatch 的投影矩阵。

@Override
public void resize(int width, int height) {
    camera.setToOrtho(false, width, height);
    game.batch.setProjectionMatrix(camera.combined);
}

您还需要使用屏幕和高度来更新舞台的视口。

对于nmsc 文本,您应该使用Label

最好使用ExtendViewprot而不是FillViewport,看看this的答案。

【讨论】:

    猜你喜欢
    • 2020-08-16
    • 2012-08-13
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多