I have done it like this:
First i have created a new Class called Hud. It implements Disposable
because of the resource management. Then you need to define a Stage
for your content and a new viewport
because a new camera will be used.
You need to define a new Table
that you can add to the Stage
. You can add your content to the table
like usual.
The rest of the code i will put to show how it works is game specific so just ignore it.
public class Hud implements Disposable{
public Stage stage;
private Viewport viewport;
//score && time tracking variables
private Integer worldTimer;
private float timeCount;
private static Integer score;
private boolean timeUp;
//Scene2D Widgets
private Label countdownLabel, timeLabel, linkLabel;
private static Label scoreLabel;
public Hud (SpriteBatch sb){
//define tracking variables
worldTimer = 250;
timeCount = 0;
score = 0;
//setup the HUD viewport using a new camera seperate from gamecam
//define stage using that viewport and games spritebatch
viewport = new FitViewport(GetTheTriforce.V_WIDTH, GetTheTriforce.V_HEIGHT, new OrthographicCamera());
stage = new Stage(viewport, sb);
//define labels using the String, and a Label style consisting of a font and color
countdownLabel = new Label(String.format("%03d", worldTimer), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
scoreLabel =new Label(String.format("%06d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
timeLabel = new Label("LEFTOVER TIME", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
linkLabel = new Label("POINTS", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
//define a table used to organize hud's labels
Table table = new Table();
table.top();
table.setFillParent(true);
//add labels to table, padding the top, and giving them all equal width with expandX
table.add(linkLabel).expandX().padTop(10);
table.add(timeLabel).expandX().padTop(10);
table.row();
table.add(scoreLabel).expandX();
table.add(countdownLabel).expandX();
//add table to the stage
stage.addActor(table);
}
public void update(float dt){
timeCount += dt;
if(timeCount >= 1){
if (worldTimer > 0) {
worldTimer--;
} else {
timeUp = true;
}
countdownLabel.setText(String.format("%03d", worldTimer));
timeCount = 0;
}
}
public static void addScore(int value){
score += value;
scoreLabel.setText(String.format("%06d", score));
}
@Override
public void dispose() { stage.dispose(); }
public boolean isTimeUp() { return timeUp; }
public static Label getScoreLabel() {
return scoreLabel;
}
public static Integer getScore() {
return score;
}
}
and then in the Playscreen like this:
first of all you need a variable to reference your hud like:
private Hud hud;
and then in the constructor you create an new instance of your class:
hud= new Hud();
in the update- method you need to put these lines of code since i think you want to display some game information like points or the lives left:
hud.update();
in the render- method do this:
//Set batch to now draw what the Hud camera sees.
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
and at the end don't forget to dispose your hud in the dispose- method
i hope that helps
camera.project(Vector3 screenCoords)
to project something from world coordinates to screen cords. \$\endgroup\$