JAVA-LIBGDX如何正确设置scrollPane
问题描述:
我试图在屏幕底部向我的游戏添加一个较少的滑动菜单(左侧和右侧)。 我的问题是,我不能在屏幕的底部调整表,这是我的代码:JAVA-LIBGDX如何正确设置scrollPane
private void initUI() {
// inizializzazione dello stage
stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()/4));
Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
Gdx.input.setInputProcessor(stage);
// inizializzazione della tabella
container = new Table();
container.setFillParent(true);
stage.addActor(container);
Table table = new Table();
table.debug();
final ScrollPane scroll = new ScrollPane(table, skin);
scroll.setFillParent(true);
InputListener stopTouchDown = new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
event.stop();
return false;
}
};
table.pad(20).defaults().expandY().space(5);
for (int i = 0; i < 15; i++) {
table.row();
TextButton button = new TextButton(i + "dos", skin);
table.add(button);
button.addListener(new ClickListener() {
public void clicked (InputEvent event, float x, float y) {
System.out.println("click " + x + ", " + y);
}
});
}
container.add(scroll).expandY().fill().colspan(1);
container.row().space(10).padBottom(10);
}
render(){
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight()/4);
Gdx.input.setInputProcessor(stage);
stage.draw();
stage.act(Gdx.graphics.getDeltaTime());
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
这样,我可以分割屏幕分为2部分(顶部和底部),但输入不正确:要激活第一个按钮,我必须按屏幕的顶部,但底部是“设计”按钮。
我怎样才能创建一个在屏幕底部的滚动表?
答
似乎视口,表的一些误解,以及如何使用它们:
使用一个Viewport
为您呈现您的整个屏幕。使用Table
分割屏幕。因此,改变initUI
到
stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()));
第一行(删除/ 4)
和调整你的渲染方法是这样的:
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.
stage.act(delta);
stage.draw();
}
清除你的颜色缓冲,以确保动画,或按钮按下操作被正确绘制。每帧调用不要将stage
设置为InputProcessor
。一旦这在initUI
完成,那就没问题。
对表的大小:我认为该表与包含的元素不匹配。因此,每当您添加一个带有table.add(button)
的按钮时,请使用table.add(button).height(...).width(...)
添加有关其高度和宽度的信息。然后表格和相应的滚动窗格调整到正确的位置。
目前我只看到container
包含scroll
对象,填充其父项填充整个屏幕。因此,在container.row().space(10).padBottom(10);
之后将scroll
添加到container
之后,请确保将其他表添加到容器中。该表格将在滚动窗格下方绘制。
答
谢谢,我已经做到了,这是我的新代码:
// inizializzazione dello stage
stage = new Stage(new ExtendViewport(Constants.VIEWPORT_GUI_WIDTH,Constants.VIEWPORT_GUI_HEIGHT/4));
Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
Gdx.input.setInputProcessor(stage);
// inizializzazione della tabella
container = new Table();
container.setFillParent(true);
container.bottom();
stage.addActor(container);
Table table = new Table();
table.debug();
table.bottom();
final ScrollPane scroll = new ScrollPane(table, skin);
//scroll.setFillParent(true);
scroll.setupFadeScrollBars(0f, 0f);
InputListener stopTouchDown = new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
event.stop();
return false;
}
};
table.pad(20).defaults().space(5);
for (int i = 0; i < 15; i++) {
TextButton button = new TextButton(i + "dos", skin);
table.add(button).height(scroll.getHeight()).width(Constants.VIEWPORT_GUI_WIDTH/8);
button.addListener(new ClickListener() {
public void clicked (InputEvent event, float x, float y) {
System.out.println("click " + x + ", " + y);
}
});
}
container.bottom();
container.add(scroll).height(Gdx.graphics.getHeight()/3);//.expandY().fill().colspan(1);
container.row().space(10).padBottom(10);