libgdx ScrollPane - 无法滚动?

5
我是一个多人游戏中的大厅制作人,这个游戏可能会在表格中显示任意数量的玩家。我有以下代码:
camera = new OrthographicCamera(WIDTH,HEIGHT);
    camera.position.set(WIDTH/2,HEIGHT/2, 0);
    camera.update();

    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    stage.setCamera(camera);
    stage.setViewport(WIDTH,HEIGHT,false);

    ScrollPaneStyle paneStyle = new ScrollPaneStyle();
    paneStyle.background = new TextureRegionDrawable(WizardsDuel.atlas.findRegion("cavebg"));
    paneStyle.vScrollKnob = new TextureRegionDrawable(WizardsDuel.atlas.findRegion("GUI/slidernob"));
    paneStyle.hScroll = paneStyle.hScrollKnob = paneStyle.vScroll = paneStyle.vScrollKnob;

    Table container = new Table();
    table = new Table();
    ScrollPane pane = new ScrollPane(table,paneStyle);
    container.add(pane).width(WIDTH).height(HEIGHT);
    container.row();
    container.setBounds(0,0,WIDTH,HEIGHT);
    stage.addActor(container);

    font = new BitmapFont();
    color = new Color(0,0,0,1);
    style = new TextButtonStyle();
    style.font = font;
    style.up = new TextureRegionDrawable(WizardsDuel.atlas.findRegion("GUI/guibg"));
    style.fontColor = color;

    handler = new ChangeHandler();
    ArrayList<User> users = FirebaseManager.lobbyUsers;
    for(int i = 0; i < users.size() || i < 100; i++){
        TextButton tmp = new TextButton("",style);
        tmp.scale(2);
        //tmp.setText(users.get(i).name);
        tmp.setText(i+ "   asdf");
        tmp.addListener(handler);
        table.add(tmp).width(WIDTH/4f);
        if(i%2 == 1 && i > 0)
            table.row();
    }

尽管表格明显延伸到屏幕底部(这应该是滚动窗格的底部),但没有滚动条,点击和拖动也没有反应。
截图: enter image description here
5个回答

8

您实际上需要两个东西:外部容器和内部容器。通常使用两个表格来实现此目的。以下是我使用scrollPane的一个小例子,没有样式,但应该很清楚。

this.table = new Table();
this.container = new Table();
scroll = new ScrollPane(table);
container.add(scroll).width(500f).height(500f);
container.row();
是一个可以滚动的表格,你需要在其中添加内容。容器则是外部盒子,你可以在滚动框上方添加标题等。外部盒子(容器)需要定义大小,否则就没有滚动条等。同时,你也需要内部表格。

以下是一个简单的添加示例:

style = new LabelStyle(font, Color.WHITE);
label = new Label(null, style);
table.add(label);
table.row();
label.setAlignment(1); // align center

如果给它更多行以显示,就会出现滚动窗格。否则没有滚动条或类似的东西。


一切都在一个中。我认为你只是错过了外部容器。添加它应该可以解决问题。不要忘记设置大小。


还有,你从哪里得到了Config.VIRTUAL_VIEW_WIDTH? - csga5000
这是我程序中使用的静态浮点数。请用一些值替换它。 - bemeyer
我使用了一个叫做WIDTH的静态字段。我将编辑我的帖子,以便显示容器。 - csga5000
我会添加一张截图。 - csga5000
是的,我有,但我刚意识到一些事情。按钮也不像我期望的那样工作。也许我的舞台没有正确地分发/接收输入...我设置了输入处理器...你可以在这里下载桌面版:http://hobogames.atspace.cc/(只需点击巫师战争,下载桌面版,看看自己的问题是什么(它是pvp菜单) - csga5000
显示剩余2条评论

8

也许您忘记在 render() 中添加阶段操作,需要添加类似以下的内容:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
    stage.draw();
}

2

我的问题是它没有接收输入。我不应该在构造函数中设置输入处理器,因为前一个屏幕在“隐藏”时会将处理器设置为null,所以当我将该行移动到LobbyScreen的show方法中时,它发生在前一个屏幕隐藏之后,输入就设置正确了。


1
在初始化滚动窗格后,您应该首先调用此函数。
    mScrollPane.layout();

0

2023 | KOTLIN

在将元素传递给ScrollPane之前,必须将要滚动的元素添加到舞台或组中

  1. 创建必要的演员并将其添加到舞台或组中

    val actor = Actor()
    [Stage/Group].addActor(actor)
    
  2. 创建一个ScrollPane并将一个演员放入其中

    val scrollPane = ScrollPane(actor)
    
  3. 为演员和ScrollPane设置位置和尺寸

    actor.setBounds(x,y,w,h) | scrollPane.setBounds(x,y,w,h)
    

附言:Vel_daN:热爱你所做的。



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