简体   繁体   English

libgdx ScrollPane - 不滚动?

[英]libgdx ScrollPane - Doesn't scroll?

I'm making a lobby in a multi-player game, which may have any number of players displayed in a table.我正在多人游戏中创建大厅,表格中可能显示任意数量的玩家。 I have the following code:我有以下代码:

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

Despite the manner in which the table clearly extends below the bottom of the screen(which should be the bottom of the scroll pane), there is no scroll bar, and clicking and dragging does nothing.尽管表格明显延伸到屏幕底部(应该是滚动窗格的底部)下方,但没有滚动条,单击和拖动也没有任何作用。

Screenshot:截屏:在此处输入图像描述

You do actually need two things. 你确实需要两件事。 An outer Container and an inner Container. 外部容器和内部容器。 You do regularly use two Tables for this. 为此,您经常使用两个表。 So here is a small example how I use the scrollPane. 所以这里有一个小例子我如何使用scrollPane。 Actually with no style but it should make it clear. 实际上没有风格,但它应该说清楚。

this.table = new Table();
this.container = new Table();
scroll = new ScrollPane(table);
container.add(scroll).width(500f).height(500f);
container.row();

The table is the table one where you do add the stuff that should be scrollable. 该表是您添加应该可滚动的内容的表。 The container is the outer box. 容器是外盒。 So you can add for example a headline above your scrollbox. 因此,您可以在滚动条上方添加标题。 The outer box (container) does need a size. 外箱(容器)确实需要一个尺寸。 Else you will not have any scrollbars or such. 否则你不会有任何滚动条等。 And you do need the inner table. 你确实需要内表。

Simple example for adding: 添加的简单示例:

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

give it more lines that it can show and you'll have a scroll pane. 给它更多的线条,它可以显示,你将有一个滚动窗格。 Else there is no scrollbar or such. 否则没有滚动条等。


All in one. 一体。 You just missed the outer container i think. 你错过了我认为的外部容器。 Add it and it should work. 添加它,它应该工作。 Don't forget to sett the sizes. 不要忘记设置尺寸。

maybe you forget to make stage act in render(), add something like: 也许你忘记在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();
}

My problem was that is wasn't receiving input. 我的问题是没有收到输入。 My I shouldn't have set the input processor in the constructor, because, the previous screen was setting the processor to null when it was "hidden", so when I moved the line to LobbyScreen's show method it happened after the previous screen was hidden, and the input is set correctly. 我不应该在构造函数中设置输入处理器,因为,前一个屏幕将处理器设置为null,当它被“隐藏”时,所以当我将该行移动到LobbyScreen的show方法时,它发生在上一个屏幕被隐藏之后,输入设置正确。

在您初始化滚动窗格后,您应首先调用它

    mScrollPane.layout();

2023 | 2023 | 年KOTLIN KOTLIN

The element to be scrolled must be added to the stage or group before passing it to the ScrollPane要滚动的元素必须先添加到舞台或组,然后再传递给 ScrollPane

  1. Create the necessary actor and add it to the stage or group创建必要的演员并将其添加到舞台或组中

    val actor = Actor() [Stage/Group].addActor(actor)
  2. Create a ScrollPane and place an actor in it创建一个 ScrollPane 并在其中放置一个 actor

     val scrollPane = ScrollPane(actor)
  3. Set the position and dimensions for the actor and the ScrollPane为 actor 和 ScrollPane 设置 position 和尺寸

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

PS.附言。 Vel_daN: Love what You DO. Vel_daN:热爱你所做的。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM