- 按钮应放置在表中
- 表格可以(且应该)嵌套
- 仅主表应填充父级。
我想要实现的是将标签(tableLabs)打包在顶部(与原始马里奥相同),并将按钮对齐到底部角落(因此,左侧按钮将位于左下角,右侧按钮将位于右下角),跳跃按钮直接位于方向按钮上方,而不考虑屏幕大小,类似于具有Java Swing边界布局。
最后,我使用简单的“->”,“<-”和“^”作为未来按钮图形的占位符。
public class Hud implements Disposable{
public Stage stage;
public Viewport viewport;
private Integer worldTimer;
private float timeCount;
private static Integer score;
Label countDownLabel;
static Label scoreLabel;
Label timeLabel;
Label levelLabel;
Label worldLabel;
Label marioLabel;
TextButton butL, butR, butJ;
public Hud(SpriteBatch spriteBatch){
worldTimer = 300;
timeCount = 0;
score = 0;
viewport = new FitViewport(SuperMario.WORLDWIDTH, SuperMario.WORLDHEIGHT, new OrthographicCamera());
stage = new Stage(viewport, spriteBatch);
Table mainTable = new Table();
// mainTable.top().center();
mainTable.setFillParent(true);
Table tableLabs = new Table();
tableLabs.top();//align to top
// table.setFillParent(true);//fill
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("TIME", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
levelLabel = new Label("1-1", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
worldLabel = new Label("WORLD", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
marioLabel = new Label("MARIO", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
tableLabs.add(marioLabel).expandX().padTop(10);
tableLabs.add(worldLabel).expandX().padTop(10);
tableLabs.add(timeLabel).expandX().padTop(10);
tableLabs.row();
tableLabs.add(scoreLabel).expandX().padTop(10);
tableLabs.add(levelLabel).expandX().padTop(10);
tableLabs.add(countDownLabel).expandX().padTop(10);
// stage.addActor(table);
//button setup
Table butTable = new Table();
butTable.bottom();
// table.setFillParent(true);
TextButton.TextButtonStyle tbs = new TextButton.TextButtonStyle();
tbs.font = new BitmapFont();
butR = new TextButton("b1", tbs);
butR.setText("->");
butR.setColor(Color.FIREBRICK);
butR.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.app.log("B-right", "pressed");
}
});
butL = new TextButton("b2", tbs);
butL.setText("<-");
butL.setColor(Color.FIREBRICK);
butL.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.app.log("B-left", "pressed");
}
});
butJ = new TextButton("b3", tbs);
butJ.setText("^");
butJ.setColor(Color.FIREBRICK);
butJ.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.app.log("B-jump", "pressed");
}
});
butTable.setDebug(true);
butTable.add(butJ).expandX().padBottom(10).left().expand().padRight(10);
butTable.add(butJ).expandX().padBottom(10).right().expand().padLeft(10);
butTable.row();
butTable.add(butL).expandX().padBottom(10).left().expand().padRight(10);
butTable.add(butR).expandX().padBottom(10).right().expand().padLeft(10);
mainTable.add(tableLabs).expand();
mainTable.row();
mainTable.add(butTable).expand();
mainTable.setDebug(true);
// stage.addActor(butTable);
stage.addActor(mainTable);
Gdx.input.setInputProcessor(stage);
}
public void update(float dt){
timeCount+= dt;
if(timeCount >=1){
worldTimer--;
countDownLabel.setText(String.format("%03d", worldTimer));
timeCount--;
}
}
public static void addScore(int value){
score += value;
scoreLabel.setText(String.format("%06d", score));
}
@Override
public void dispose() {
stage.dispose();
}
}
