gpt4 book ai didi

Libgdx ScrollPane 在顶部添加空间

转载 作者:行者123 更新时间:2023-12-02 03:35:14 28 4
gpt4 key购买 nike

我正在为我的游戏创建一个商店,但我遇到了 libgdx ScrollPane 问题。它似乎将其中的表格向下移动了 155 像素。它所产生的效果是,表格显示在距顶部大约 155 像素的位置,并且无论我向下滚动多远,最后一个按钮都会离开屏幕。

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

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,最后一个按钮也始终部分离开屏幕。

根据此处的要求,我创建了视口(viewport)和相机的代码:

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

最佳答案

好吧,终于明白了。结果 ScrollPane 中的 Table 无法设置为填充父级。

table.setFillParent(true);

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

关于Libgdx ScrollPane 在顶部添加空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27577976/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com