java – 具有多个小部件的LibGDX和ScrollPane

前端之家收集整理的这篇文章主要介绍了java – 具有多个小部件的LibGDX和ScrollPane前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试将多个项目添加到滚动条中,我很快发现所有“addActor”功能不支持.所以,我添加了一个表,我想要的所有项目(这个代码错过了我仍然想添加的图像)来创建一个可滚动的信用屏幕…但这种方法(当前)不允许溢出,渲染ScrollPane没用. (我的文字显示到屏幕高度允许的位置,不可滚动).
在LibGDX中创建带有多个小部件的可滚动窗格的方法是什么? (我目前只关心 Android和Win / Lin / Mac平台.
pane = new ScrollPane(null,skin);
    pane.setFillParent(true);
    paneContent = new Table(skin);
    paneContent.setFillParent(true);
    Label temp = new Label("",skin);
    temp.setAlignment(Align.left,Align.center);
    temp.setText( Gdx.files.internal("licenses/credits.txt").readString("UTF-8") );
    paneContent.addActor(temp);
    pane.setWidget(paneContent);
    stage.addActor(pane);

解决方法

如果要将多个项目放入ScrollPane,您只需要在其中放置一个表,并为要放入ScrollPane的每个小部件调用add().

以下是如何使您的信用可滚动的示例:

public class ScrollTest implements ApplicationListener {
    private Stage stage;

    private static final String reallyLongString = "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
        + "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
        + "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n";

    @Override public void create() {
        this.stage = new Stage();
        Gdx.input.setInputProcessor(this.stage);
        final Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));

        final Label text = new Label(reallyLongString,skin);
        text.setAlignment(Align.center);
        text.setWrap(true);
        final Label text2 = new Label("This is a short string!",skin);
        text2.setAlignment(Align.center);
        text2.setWrap(true);
        final Label text3 = new Label(reallyLongString,skin);
        text3.setAlignment(Align.center);
        text3.setWrap(true);

        final Table scrollTable = new Table();
        scrollTable.add(text);
        scrollTable.row();
        scrollTable.add(text2);
        scrollTable.row();
        scrollTable.add(text3);

        final ScrollPane scroller = new ScrollPane(scrollTable);

        final Table table = new Table();
        table.setFillParent(true);
        table.add(scroller).fill().expand();

        this.stage.addActor(table);
    }

    @Override public void render() {
        this.stage.act();
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        this.stage.draw();
    }

    @Override public void resize(final int width,final int height) {}
    @Override public void pause() {}
    @Override public void resume() {}
    @Override public void dispose() {}
}

编辑:添加了在ScrollPane中设置表格的代码.

猜你在找的Java相关文章