LibGDX Nesting Tables for gameboard grid

天涯浪子 提交于 2019-12-12 01:19:10

问题


I am playing with LibGDX and trying to set up a simple board game.

public void show() {
    width       = Gdx.graphics.getWidth();
    height      = Gdx.graphics.getHeight();
    viewport    = new FitViewport(STAGE_WIDTH, STAGE_HEIGHT);
    stage       = new Stage(viewport);
    sceneBoard  = new GridSceneBoard(10, 30, GridBoardCell.Type.CHECKERED);
    rootTable   = new Table();

    rootTable.setFillParent(true);
    sceneBoard.setFillParent(true);

    rootTable.add(sceneBoard);

    stage.addActor(rootTable);
    Gdx.input.setInputProcessor(stage);
}

a GridSceneBoard extends a Table to conveniently make a grid of images like a chess board. The render method is as follows:

public void render(float delta) {
    // Set black background.
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // Uncomment for table lines during debugging.
    rootTable.debug();
    sceneBoard.debug();
    Table.drawDebug(stage);

    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}

I want to nest the tables for better UI layout (eventually), but when I run the code I get:

I've read TableLayout's documentation as well as LibGDX's scene2d.ui documentation, what am I doing wrong? I am using LibGDX 1.0.


回答1:


Its very clear the documentation [1] that

Normally a widget's size is set by its parent and setFillParent must not be used. setFillParent is for convenience only when the widget's parent does not set the size of its children (such as the stage).

So, I basically should never have sceneBoard.setFillParent(true). Only the root table should fill its parent.

[1] https://github.com/libgdx/libgdx/wiki/Scene2d.ui#stage-setup



来源:https://stackoverflow.com/questions/23483546/libgdx-nesting-tables-for-gameboard-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!