gpt4 book ai didi

java - 将文本按钮居中到屏幕 libgdx

转载 作者:太空狗 更新时间:2023-10-29 15:53:37 26 4
gpt4 key购买 nike

我目前正在使用 LibGDX 创建游戏,并且正在创建我的标题屏幕。我在屏幕上将 TextButtons 居中时遇到问题,如果将 TextButton 的宽度设置为舞台的宽度,我可以使它们居中,但是如果您从左到右单击该按钮的任何地方都允许按下按钮而不是如果您只需点击按钮本身。

如何让 TextButtons 以游戏屏幕为中心并彼此居中?我有三个文本按钮(新游戏、继续和退出游戏)。

非常感谢任何帮助,谢谢!

编辑:

这是我的三个按钮的代码。

btnNG = new TextButton("New Game", btnStyle[0]) {
{
setName("New Game");
addListener(new InputListener() {
public boolean touchDown(InputEvent e, float x, float y, int pointer, int button) {
g.debugOut(btnNG.getName(), "touchDown?");
return true;
}

public void touchUp(InputEvent e, float x, float y, int pointer, int button) {
g.debugOut(btnNG.getName(), "touchUp?");
//g.switchScreen(new Mainscreen(g));
}
});
setWidth(sTitle.getWidth());
setPosition(0, (getHeight() * 2.5f));
}
};

btnContinue = new TextButton("Continue", btnStyle[0]) {
{
setName("Continue");
addListener(new InputListener() {
public boolean touchDown(InputEvent e, float x, float y, int pointer, int button) {
g.debugOut(btnContinue.getName(), "touchDown?");
return true;
}

public void touchUp(InputEvent e, float x, float y, int pointer, int button) {
g.debugOut(btnContinue.getName(), "touchUp?");
}
});
setWidth(sTitle.getWidth());
setPosition(0, (getHeight() * 1.4f));
}
};

btnExit = new TextButton("Exit Game", btnStyle[0]) {
{
setName("Exit Game");
addListener(new InputListener() {
public boolean touchDown(InputEvent e, float x, float y, int pointer, int button) {
g.debugOut(btnExit.getName(), "touchDown?");
return true;
}

public void touchUp(InputEvent e, float x, float y, int pointer, int button) {
g.debugOut(btnExit.getName(), "touchUp?");
//Gdx.app.exit();
}
});
setWidth(sTitle.getWidth());
setPosition(0, (getHeight() * .3f));
}
};

这就是我目前所拥有的,它是居中的,但这样做可以通过按屏幕上实际文本左侧/右侧的任意位置来单击它。

最佳答案

我猜 sTitle 是你的舞台对象......


你是直接在舞台上添加按钮吗?

如果是这样,我建议使用表格。

Table menuTable = new Table();
menuTable.add(btnNG);
menuTable.row();
menuTable.add(btnContinue);
menuTable.row();
menuTable.add(btnExit);
menuTable.setFillParent(true);
sTitle.addActor(menuTable);

注1:
删除所有按钮上的所有 setSize 和 setPosition 调用。
删除其他 addActor 调用。

注2:
有关高级功能,请参阅 TableLayout

关于java - 将文本按钮居中到屏幕 libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20731886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com