Libgdx和Android Studio:标签不显示任何文本

时间:2018-07-02 03:07:55

标签: java android libgdx label

我正在尝试在屏幕上添加标签文本,但是似乎什么都没有显示:这是我编码的代码,下面是我最终使用的屏幕,我仍然可以更改所有颜色,但是什么也没显示上

    import com.badlogic.gdx.ApplicationAdapter;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.BitmapFont;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.scenes.scene2d.ui.Label;
    import com.badlogic.gdx.scenes.scene2d.ui.Skin;
    import com.badlogic.gdx.utils.Align;
    import com.badlogic.gdx.utils.viewport.ScreenViewport;
    import com.badlogic.gdx.utils.viewport.Viewport;

public class FiraMain extends ApplicationAdapter {
    SpriteBatch batch;
    private Skin mySkin;
    private Stage stage;

@Override
public void create () {
    batch = new SpriteBatch();
    stage = new Stage(new ScreenViewport());
    int guides = 12;
    int rowHeight = Gdx.graphics.getWidth() / 12;
    int colWidth = Gdx.graphics.getHeight() / 12;

    Label.LabelStyle labelStyle = new Label.LabelStyle();
    Skin mySkin = new Skin(Gdx.files.internal("skin/glassy-ui.json"));
    Label label = new Label("Text", mySkin);
    label.setSize(Gdx.graphics.getWidth() / guides, rowHeight);
    label.setPosition(colWidth * 2, Gdx.graphics.getHeight() - rowHeight * 6);
    stage.addActor(label);

}

  @Override
  public void render () {
      Gdx.gl.glClearColor(1, 1, 0, 0);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      batch.begin();
      batch.end();
   }

   @Override
   public void dispose () {

   }
}

Image

1 个答案:

答案 0 :(得分:0)

我认为您误忘了调用舞台的绘制方法。

@Override
public void render () {
    Gdx.gl.glClearColor(1, 1, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.draw();  //<--  Call draw method of stage inside render, it calls draw on every actor in the stage 
    stage.act();
}

我还建议您在游戏中处理所有一次性元件。

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