将文本按钮居中到屏幕libgdx

时间:2013-12-22 17:13:23

标签: java android libgdx

我目前正在使用LibGDX创建游戏,目前我正在创建我的标题屏幕。我无法在屏幕上居中显示TextButtons,如果将TextButton的宽度设置为舞台的宽度,我可以让它们居中,但是如果你点击该按钮从左到右的任何地方都可以按下按钮而不是你只是点击按钮本身。

如何将TextButtons置于游戏界面的中心并相互居中?我有三个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));
    }
};

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

3 个答案:

答案 0 :(得分:6)

我猜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

答案 1 :(得分:2)

您可以将它们放入Table容器中,并允许它注意对齐:

Table container = new Table();
container.add(btnNG).expandX().center().row();
container.add(btnContinue).expandX().center().row();
container.add(btnExit).expandX().center().row();
// add container to some Stage

此外,您可以在每个padTop(x)后调用center()来定义偏移量。 请阅读更多关于表格布局的here

答案 2 :(得分:1)

将每个按钮的x设置为相同,以便它们对齐。将x位置设置为屏幕宽度的一半减去按钮宽度的一半,如下所示 - > (widthOfScreen / 2) - (buttonWidth / 2)