Libgdx ScrollPane在顶部添加了空白间距

5
我正在为我的游戏创建一个商店,但使用libgdx的ScrollPane时出现了问题。它似乎将其中的表格向下移动了155像素。这导致表格大致出现在距顶部155像素的位置,并且无论我向下滚动多少,最后一个按钮都会超出屏幕。

这是“商店”首次出现时的窗口。

滚动窗格顶部 您可以看到表格的顶部相对于窗口顶部相差很大。以下是代码,但表格只有10像素的填充。

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

滚动窗格底部 您可以看到“主菜单”按钮大约有一半超出了屏幕。

这是布局“商店”的代码。

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);
    }
}

@Townsnflok,我认为错误可能在代码的另一个部分,因为我认为该代码显示的行为不反映您得到的行为,也许您已经在某个时候修改了stage.getCamera、batch或类似的东西,可以检查一下您的代码并将其添加到问题中。另外,我建议您关注一下viewport,这只是我的意见。 - Angel Angel
我会添加相机和视口设置,但实际上我不会进行修改,只是使用最小尺寸设置ExtendViewport。 - Townsfolk
1个回答

12

好的,最后终于搞清楚了。原来是ScrollPane中的Table无法设置为填充父级。

table.setFillParent(true);

导致问题出现的原因不确定,但是移除 setFillParent(true) 可以解决该问题。


即使使用LibGDX 1.9.3,这仍然是一个问题——我在ScrollPane中放置了一个带有setFillParent的Label,它在顶部有一个巨大的间隙。移除setFillParent,问题解决了。谢谢! - Max

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接