【问题标题】:How can I change font color after its has created创建后如何更改字体颜色
【发布时间】:2014-07-19 18:45:01
【问题描述】:

我正在使用 java、libgdx、scence2d。

我在构造函数中设置了 bitMapFont。现在如何在创建后更改字体颜色?我想改变渲染方法。

这是我为标题和菜单项(3 个按钮)创建 BitmapFont 的构造函数

public menuScreen() {
    sb = new SpriteBatch();

    stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),
            true);
    bitMapFont = new BitmapFont(Gdx.files.internal("font/font.fnt"), false);
    labelStyle = new LabelStyle(bitMapFont, Color.WHITE);
    label = new Label("Asteroids", labelStyle);
    float width = bitMapFont.getBounds(title).width;
    label.setPosition((Gdx.graphics.getWidth() - width) / 2,
            (Gdx.graphics.getHeight()) - 100);
    stage.addActor(label);


    //menu items
    menuItems = new String[] { "Play", "HighScores", "Quit" };
    label2 = new Label(menuItems[0], labelStyle);
    label2.setPosition((Gdx.graphics.getWidth() - width) / 2,
            (180 - 35 * 0));

    label3 = new Label(menuItems[1], labelStyle);
    label3.setPosition((Gdx.graphics.getWidth() - width) / 2,
            (180 - 35 * 1));

    label4 = new Label(menuItems[2], labelStyle);
    label4.setPosition((Gdx.graphics.getWidth() - width) / 2,
            (180 - 35 * 2));

    stage.addActor(label2);
    stage.addActor(label3);
    stage.addActor(label4);
}

绘制标题和菜单项

public void draw() {
    sb.setProjectionMatrix(MyGdxGame.camera.combined);
    sb.begin();


   //if user press 'Play' menu item
   if(currentItem == 1){
       //How to change label2 color??????
   }

    // update and draw stage
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();

    sb.end();
}

【问题讨论】:

  • 能否请您提及这是哪种语言.. 也用该语言标记问题

标签: java fonts libgdx scene2d


【解决方案1】:

如果您从不打算更改任何一个标签的颜色,那么只需给它一个不同的 LabelStyle,并在 LabelStyle 中指定所需的颜色。

如果您想随意更改标签的颜色,请在设置标签的新文本之前使用label.getBitmapFontCache().setColor()

【讨论】:

    猜你喜欢
    • 2015-05-18
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2011-06-19
    • 1970-01-01
    • 2018-04-05
    • 2015-06-09
    相关资源
    最近更新 更多