如何更改LibGDX表中的路线?

时间:2017-04-14 23:21:32

标签: java libgdx

我在 libgdx 中有一个表格,代表游戏的屏幕控制器。其中两个按钮位于左侧,一个按钮位于右侧。当我试图让桌子切换到右侧时,它仍然会在左侧结束。

public class Controller {

    Viewport viewport;
    Stage stage;

    boolean upPressed;
    boolean downPressed;
    boolean leftPressed;
    boolean rightPressed;

    OrthographicCamera cam;

    public Controller(){
        cam = new OrthographicCamera();
        viewport = new FitViewport(800, 480, cam);

        stage = new Stage(viewport, OverlayDemo.batch);
        Gdx.input.setInputProcessor(stage);

        Table table = new Table();
        table.left().bottom();

        Image upImage = new Image(new Texture("flatDark25.png"));
        upImage.setSize(80, 80);
        upImage.addListener(new InputListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                upPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                upPressed = false;
            }
        });

        Image leftImage = new Image(new Texture("flatDark23.png"));
        leftImage.setSize(80, 80);
        leftImage.addListener(new InputListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                leftPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                leftPressed = false;
            }
        });

        Image rightImage = new Image(new Texture("flatDark24.png"));
        rightImage.setSize(80, 80);
        rightImage.addListener(new InputListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                System.out.println("Right pressed");
                rightPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                rightPressed = false;
            }
        });

        table.bottom().left();
        table.pad(10);
        table.add(leftImage).size(leftImage.getWidth(), leftImage.getHeight());
        table.add(rightImage).padLeft(20).size(rightImage.getWidth(), rightImage.getHeight());

        table.bottom().right();
        table.pad(10);
        table.add(upImage).size(upImage.getWidth(), upImage.getHeight());

        table.pack();

        stage.addActor(table);
    }


    public void draw(){
        stage.draw();
    }

    public boolean isUpPressed() {
        return upPressed;
    }

    public boolean isRightPressed() {
        return rightPressed;
    }

    public boolean isDownPressed() {
        return downPressed;
    }

    public boolean isLeftPressed() {
        return leftPressed;
    }

    public void resize(int width, int height){
        viewport.update(width, height);
    }
}

在我呼叫.bottom().right()的部分,它应该将对齐切换到右侧。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的错误在于您正在调整Table本身而不是基础Cell。应该在add(actor)之后调用align方法,因此您的代码应该看起来像这样

table.add(leftImage).size(leftImage.getWidth(), leftImage.getHeight()).pad(10).bottom().left();
table.add(rightImage).padLeft(20).size(rightImage.getWidth(), rightImage.getHeight()).pad(10).bottom().left();

table.add(upImage).size(upImage.getWidth(), upImage.getHeight()).pad(10).bottom().right();

table.pack();

stage.addActor(table);

我还建议你拨打table.setFillParent(true),因为它似乎是根表。 要进一步阅读表格,您可以查看libGDX Wiki