我这样做是这样的:
首先,我创建了一个名为Hud的新类。它Disposable
是由于资源管理而实现的。然后,您需要Stage
为您的内容定义一个和一个新的,viewport
因为将使用一个新的相机。您需要定义一个Table
可以添加到中的新内容Stage
。您可以table
像往常一样添加内容。我将要展示的其余代码是特定于游戏的,因此只需忽略它即可。
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;
}
}
然后在Playscreen中像这样:
首先,您需要一个变量来引用您的hud,例如:
private Hud hud;
然后在构造函数中创建类的新实例:
hud= new Hud();
在update-方法中,您需要放置以下代码行,因为我认为您想显示一些游戏信息,例如积分或剩余生命:
hud.update();
在render-方法中执行以下操作:
//Set batch to now draw what the Hud camera sees.
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
最后,别忘了使用dispose-方法处理您的平视显示器
我希望有帮助
camera.project(Vector3 screenCoords)
投影从世界坐标到屏幕线的物体。