java智能算法_java – 智能和自动按钮放置

我正在使用LibGDX按钮进行触摸输入(以前使用的桌面模式和键盘按键.到目前为止,按钮监听器按预期工作,但我无法按照我想要的方式放置这些按钮.我试图查看文档和一些这些例子并不能解决这里的问题,但从我读过的几件关键事情我已经注意到:

>按钮应放在表格中

>表可以(也应该)嵌套

>只有主表应填充父表.

我想要完成的是将标签(tableLabs)打包在顶部(根据原始Mario)和按钮与底角对齐(因此按钮左侧将位于左下角,按钮位于右下方)跳跃按钮直接位于方向按钮上方而不考虑屏幕尺寸,类似于具有Java Swing边框布局.

最后,我使用简单的“ – >” ,“< – ”和“^”作为未来按钮图形的占位符. 到目前为止,结果如下:

Tup1D.jpg

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();

}

}

最佳答案 设置表格单元格时缺少的是填充参数.调用像你正在进行的单元格扩展使得单元格拉伸适合,但单元格内的小部件不会变大,除非你还调用单元格上的填充.

因此,将两个内部表添加到主表的底部附近的部分应如下所示:

mainTable.add(tableLabs).expand().fill(); //stretch the inner table to the size of the cell

mainTable.row();

mainTable.add(butTable).expand().fill(); //as above

mainTable.setDebug(true);

stage.addActor(mainTable);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值