Libgdx ScrollPane在顶部添加空间

时间:2014-12-20 07:13:36

标签: libgdx scene2d scrollpane

我正在为我的游戏创建商店,而且我遇到了与libgdx ScrollPane有关的问题。它似乎正在将其中的表格向下移动155px。它的效果是表格从顶部向下显示大约155px,最后一个按钮离开屏幕,无论我向下滚动多远。

这是" Store"第一次出现。

Top of the scroll pane 正如您所看到的,桌子的顶部位于窗口顶部的下方。代码如下,但该表只有10px的填充。

这是我尽可能向下滚动的窗口 - 抱歉,在我抓住屏幕截图之前,滚动条消失了。

Bottom of the scroll pane 你可以看到"主菜单"按钮距屏幕大约一半。

以下是列出" Store"。

的代码
table = new Table();
table.setFillParent(true);
table.defaults().expandX().fill().space(5f);
table.pad(10f);

// characters
table.add(new Label("Characters: ", skin, "default")).left().colspan(2);
table.row();
ButtonGroup characterGroup = new ButtonGroup();
characterGroup.setMaxCheckCount(1);
Button boyButton = createImageTextButton("Plain Boy", boyTexture);
Button catButton = createImageTextButton("Cat Girl", catTexture);
Button hornButton = createImageTextButton("Horn Girl", hornTexture);
Button pinkButton = createImageTextButton("Pink Girl", pinkTexture);
Button queenButton = createImageTextButton("Queen", queenTexture);
float minHeight = 130f; // only used to force scrolling.
table.add(boyButton).minHeight(minHeight);
table.add(catButton).minHeight(minHeight).row();
table.add(hornButton).minHeight(minHeight);
table.add(pinkButton).minHeight(minHeight).row();
table.add(queenButton).minHeight(minHeight).colspan(2);
table.row();
characterGroup.add(boyButton, catButton, hornButton, pinkButton, queenButton);
characterGroup.setChecked("Plain Boy");

// drills
ButtonGroup drillGroup = new ButtonGroup();
Button flappyButton = createImageTextButton("Flappy", null);
Button tappyButton = createImageTextButton("Tappy", null);
Button tiltyButton = createImageTextButton("Tilty", null);
table.add(new Label("Drills:", skin, "default")).left().padTop(20).colspan(2);
table.row();
table.add(flappyButton).minHeight(minHeight);
table.add(tappyButton).minHeight(minHeight).row();
table.add(tiltyButton).minHeight(minHeight);
drillGroup.add(flappyButton, tappyButton, tiltyButton);
drillGroup.setChecked("Flappy");

// main menu button
table.row();
Button mainMenuButton = createImageTextButton("Main Menu", null);
table.add(mainMenuButton).minHeight(minHeight).padTop(40).padBottom(10f).colspan(2);

ScrollPane scrollPane = new ScrollPane(table, skin);
scrollPane.setBounds(0, 0, stage.getWidth(), stage.getHeight());
addActor(scrollPane);

我调试了桌面版本,在滚动之前看到表格的y位置设置为-155,滚动到底部后设置为0。但是,即使将其设置为0,最后一个按钮也总是部分在屏幕外。

这里要求的是我创建视口和摄像头的代码:

package com.example;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.example.stages.GameStage;

public class MainGame extends ApplicationAdapter {

    public static final int MIN_WIDTH = 480;
    public static final int MIN_HEIGHT = 800;

    public static SpriteBatch batch;
    public static GameStage game;
    public static Skin skin;
    public static Viewport viewport;
    public static Stage stage;

    @Override
    public void create() {
        viewport = new ExtendViewport(MIN_WIDTH, MIN_HEIGHT);
        batch = new SpriteBatch();
        initSkin();
        game = new GameStage(viewport, batch, skin);
        setStage(game);
    }

    @Override
    public void dispose() {
        super.dispose();
        game.dispose();
        skin.dispose();
    }

    public void initSkin() {
        skin = new Skin(Gdx.files.internal("skins/uiskin.json"));
    }

    @Override
    public void render() {
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

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

    public Skin getSkin() {
        return skin;
    }

    public static void setStage(Stage stage) {
        MainGame.stage = stage;
        Gdx.input.setInputProcessor(stage);
    }
}

1 个答案:

答案 0 :(得分:9)

好的,终于明白了。事实证明,Table中的ScrollPane无法设置为填充父级。

table.setFillParent(true);

导致问题。不确定为什么会这样做,但删除setFillParent(true)会解决问题。