定位按钮绝对x,y

时间:2014-08-07 17:56:59

标签: java libgdx

我一直在使用libgdx&我无法弄清楚如何通过坐标定位按钮。

public void create() {
    batch = game.getSpriteBatch();
    background = new Texture("data/textures/menuScreen_small.png");

    bubbleArray = new Array<Bubble>();

    Gdx.input.setInputProcessor(stage);

    for (int i = 0; i < 20; i++) {
        bubbleArray.add(new Bubble(MathUtils.random(0,
                Gdx.graphics.getWidth()), MathUtils.random(0,
                Gdx.graphics.getHeight())));
    }

    table = new Table();
    stage.addActor(table);
    table.setSize(1280, 720);
    table.setPosition(0, 0);
    //table.left().top();
    // table.debug();
    //table.row().minHeight(55).maxHeight(55);
    skin = new Skin(Gdx.files.internal("data/ui/uiskin.json"));

    /*
     * HomeScreen [Button 1]
     */
    final TextButton camButton = new TextButton("Camera", skin);
    //Add button
    table.add(camButton);//.left().expandX().fillX();
    camButton.setPosition(0, 0);
    //Button Height
    //table.row().minHeight(55).maxHeight(55);
    //Button Width
    //table.row().minWidth(55).maxWidth(55);

在上面的代码中,我试图将textButton“camButton”定位在屏幕上的0,0。

当我启动程序时,它位于屏幕的正中央。

如果有人有一些指南或提示让我知道,我需要学习这些东西。

小'修复'

不完全是一个修复;但通过删除表一般解决了这个问题。通过这样做,我将它添加到舞台而不是舞台&gt;表&gt;按钮。对此有何评论?

2 个答案:

答案 0 :(得分:0)

如果您只想将按钮固定到绝对位置(如0,0),则无需将项目添加到表格中,只需将按钮添加到舞台

    TextButton button = new TextButton("Floating Button", skin);
    button.setPosition(10,  10);
    stage.addActor(button);

修改的 如果您仍然希望表格提供的受控定位,请查看表格布局的指南,它有很多很好的示例Table Layout

如果你想摆放桌子

table.left().bottom();

将项目放在表格中

table.add(button).expand().bottom().right();

答案 1 :(得分:0)

我有你的问题。我对整个屏幕或部分屏幕的要求是。我使用WidgetGroup布局解决了我的问题。

actor.setPosition(100,200);
yourWidgetGroup.addActor(actor);