【问题标题】:Libgdx scene2dui column sizing issueLibgdx scene2dui 列大小问题
【发布时间】:2015-09-17 11:17:19
【问题描述】:

我正在使用滚动窗格在 libgdx 中制作一个级别表。这里有在特定滚动窗格上具有特定级别的不同表格。有什么方法可以将特定表预设为我看到的第一个表,因为如果我有超过 50 个级别,那将是愚蠢的,我必须滚动所有这些表才能最终到达特定级别。

关于我的表格和滚动窗格的简要详细信息。

我有一个称为列表的表数组列表,用于在不同列中存储不同级别的详细信息。然后将此列表添加到另一个称为 levleTable 的表中。

级别表被添加到滚动窗格中,滚动窗格被添加到主表中。

public void setScrollPane() {
    scrollPane = new ScrollPane(levelTable, getScrollPaneStyle());
    // scrollPane = new ScrollPane(levelTable, skin);
    //scrollPane.setFlickScroll(true);
    scrollPane.setFadeScrollBars(false);
    scrollPane.setScrollingDisabled(false, true);
    scrollPane.setScrollX(300);
    scrollPane.updateVisualScroll();
    //scrollPane.setSmoothScrolling(true);
    mainTable.add(scrollPane).expand();
}

在构造函数中`

    mainTable = new Table();
    mainTable.setFillParent(true);
    levelTable = new Table();
    setScrollPane();
    columnTable = new ArrayList<Table>();
    levelSelectionButton = new ArrayList<TextButton>();
    makeLevelMenu();

    //adding main table to stage
    stage.addActor(mainTable);`

【问题讨论】:

    标签: libgdx scene2d scrollpane


    【解决方案1】:

    为了更好的主意(当使用这样的机制时),我将 scrollPane 自动滚动到当前对象(例如,可能是您的下一个级别的表)。

    您可以使用以下方法轻松实现:

        scrollPane.setScrollY(y);
    

    y 应该在哪里

    y = (total height of scroll Pane content) - ( (desired element Y position inside scrollPane) - (element height) )
    

    我的意思是,如果您的 scrollPane 内容的高度为 100px,并且每个元素的高度为 20px,那么滚动到第三个元素是:

    y = 100 - (3 * 20px - 20px) = 100 - 40px = 60px
    

    如果你的 scrollPane 是水平排列的,同样会起作用 - 你只需要使用 scrollPane.setScrollX(x) 并计算所有宽度而不是高度

    更新:在使用 setScrollX / setScrollY 之前,您必须在 scrollPane 上调用 .layout() 方法


    例子:

        @Override
        public void show() 
        {
            //creating stage and viewport
            viewport = new FillViewport(1280, 800);
            stage = new Stage(viewport);
    
            Gdx.input.setInputProcessor(stage);
    
            //creating levelTable
            Table levelTable = new Table();
            levelTable.setSize(2000, 500);
    
            //filling levelTable with fake actors (it can be labels of your levels numbers)
            for(int i = 0; i < 100; i++)
            {
                Actor a = new Actor();
                a.setSize(80, 200);
    
                //set debug to see the outline
                a.debug();
    
                levelTable.add(a).pad(150, 10, 150, 10);
            }
    
            //set debug to see the outline
            levelTable.debug();
    
            //defining scroll
            ScrollPane scrollPane = new ScrollPane(levelTable, skin);
    
            scrollPane.setSize(800, 600); //your custom size
            scrollPane.setPosition(50, 50); //your custom position
    
            scrollPane.setFadeScrollBars(false);
            scrollPane.setScrollingDisabled(false, true);
    
            //important!! you have to call it before setScrollX
            scrollPane.layout(); //that's the thing!
    
            //the number of column you want to go to
            int numberOfColumn = 10;
    
            //now move to column no numberOfColumn
            scrollPane.setScrollX( levelTable.getWidth() - ( levelTable.getColumns() - numberOfColumn - 1 ) * 100 ); //100, because column width is 100 - actor = 80 + 2 * 10 pad
    
            //set debug to see the outline
            scrollPane.debug();
    
            this.stage.addActor(scrollPane);
        }
    

    【讨论】:

    • 有什么方法可以完全获取内容宽度,抱歉我之前没有提到,但我的桌子是水平排列的。
    • 检查更新如何计算整个高度/宽度
    • 对不起,伙计!我无法设置 scrollX。它只是不动。
    • 它应该可以工作 - 我在我的项目中使用相同但使用 Y。您是否尝试使用一些硬编码参数来 setScrollX()?尝试调用 .setScrollX(100)、.setScrollX(-100)、.setScrollX(20) 等,看看它是否能正常工作 - 还有你试图 setScrollX() 的地方 - 确定调用它吗?最后一件事是尝试评论 setFlick... 和其他“集合” - 也许其中一个正在阻止这个?
    • 是的,我做到了——正如我所说,我不太确定“设置...”功能——我只使用了 setScrollingDisabled——也许这里有问题?您也可以尝试使用 container.setScrollPercentX(percentX); 而不是 setScrollX() not setX() - 抱歉上面的评论有误
    猜你喜欢
    • 2015-12-06
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    相关资源
    最近更新 更多