【问题标题】:Libgdx font issueLibgdx 字体问题
【发布时间】:2023-04-03 10:15:01
【问题描述】:

我正在尝试在开始菜单的按钮上绘制文本。

我可以画按钮和除字体/文本以外的所有内容。

这是字体代码:

正在初始化:

    buttonFont = new BitmapFont(Gdx.files.internal(
                                "StartMenu/gamefont.fnt"), false);

    buttonFont.setScale(0.2f);

这里是我画舞台的地方,舞台里面有字体:

    Gdx.gl20.glClearColor(1, 0, 0, 1);

    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.act(delta);

    sb.begin();

    stage.draw();

    sb.end();

这是用于初始化更多东西的东西:

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

    if(stage == null)
        stage = new Stage();

        stage.clear();

        Gdx.input.setInputProcessor(stage);

        style.font = buttonFont;

        style.up = skin.getDrawable("Button");

        style.down = skin.getDrawable("Button");

        startGameBtn = new TextButton("Start Game", style);

        startGameBtn.setWidth(250);

        startGameBtn.setHeight(150);

        startGameBtn.setX(Gdx.graphics.getWidth() / 2 - startGameBtn.getWidth() / 2);

        startGameBtn.setY((float) (Gdx.graphics.getHeight() / 1.5));

        startGameBtn.addListener(new InputListener() 
        {


            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
            {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button)
            {
                game.setScreen(new Gameplay(game));
            }


        });

        stage.addActor(startGameBtn);

        stage.addActor(lb);

    }

非常感谢任何帮助! :)

【问题讨论】:

  • 你有理由不使用TextButton吗?
  • 我正在使用文本按钮
  • 哦,抱歉,没看到...您使用的是哪个 Libgdx 版本?好像他们在最新版本中改变了一些关于 BitmapFont 的东西......
  • 您的问题有任何更新吗?你试过我的解决方案了吗?那样有用吗 ?如果没有,你观察到什么?
  • 我还没有修好,没有。我相信字体文件可能有问题。你的解决方案是什么?我没看到。

标签: java text libgdx


【解决方案1】:

我觉得你在 resize() 中创建按钮很奇怪。

我的建议和对我有用的是初始化create() 中的所有内容,在render() 中绘制舞台并将监听器放入show()

初始化

create(){
    //Define the stage where you'll draw the TextButton
    stage = new Stage();
    //Define your font
    buttonFont = new BitmapFont(Gdx.files.internal("StartMenu/gamefont.fnt"), false);
   //Define the skin that contains your images
   textureAtlas = game.assets.get("Images.pack", TextureAtlas.class);
   skin.addRegions(textureAtlas);

   //Define the TextButtonStyle
   textButtonStyle = new TextButtonStyle();
   textButtonStyle.up = skin.getDrawable("Button");
   textButtonStyle.down = skin.getDrawable("Button");
   textButtonStyle.font =  buttonFont;
   textButtonStyle.fontColor = Color.WHITE;
   textButtonStyle.downFontColor = new Color(0.5f, 0.5f, 0.5f, 1);

   //Define the TextButton
   startGameBtn = new TextButton("Start Game", textButtonStyle);
   startGameBtn.setWidth(250);
   startGameBtn.setHeight(150);
   startGameBtn.setX(Gdx.graphics.getWidth() / 2 - startGameBtn.getWidth() / 2);
   startGameBtn.setY(Gdx.graphics.getHeight() / 1.5f);   

   //Add the TextButton to the stage
   stage.addActor(startGameBtn);
}

TextButton 的绘制

render(){
    Gdx.gl20.glClearColor(1, 0, 0, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act();
    stage.draw();
}

使用文本按钮

show(){
    startGameBtn.addListener(new InputListener(){
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
        {
            return true;
        }

        public void touchUp(InputEvent event, float x, float y, int pointer, int button)
        {
            game.setScreen(new Gameplay(game));
        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2017-01-31
    相关资源
    最近更新 更多