【问题标题】:libGDX ScrollPane with WidgetGroup instead of Table as the ScrollPane widget?libGDX ScrollPane 与 WidgetGroup 而不是 Table 作为 ScrollPane 小部件?
【发布时间】:2016-03-31 07:27:41
【问题描述】:

据我了解,ScrollPane 可以通过使用 Table 作为 ScrollPane 小部件来添加多个小部件(这是在 libGDX 存储库中的 ScrollPane 测试中完成的方式)。

我想实现类似的东西,但希望在 ScrollPane 小部件中对许多演员进行绝对定位,而不是通过使用 Table 作为 ScrollPane 小部件提供的表格定位。

我最终得到了这个不起作用的代码,但根据 libGDX javadoc 它应该,但我不知道为什么它不起作用!

stage = getStage();

// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Game.getWidth(), Game.getHeight());

// Scroll pane inner container
WidgetGroup widgetGroup = new WidgetGroup();
widgetGroup.setFillParent(true);
widgetGroup.addActor(new Image(MAP_TEXTURE_ATLAS.findRegion("map")));

// Scroll pane
ScrollPane scrollPane = new ScrollPane(widgetGroup);
container.setActor(scrollPane);

stage.addActor(container);

屏幕上什么也没有显示;

但它确实适用于以下代码(这显然不是我想要的,因为 ScrollPane 小部件是一个只能有一个演员的容器)

// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Match3.getWidth(), Match3.getHeight());

// Scroll pane inner container
Container container2 = new Container();
container2.setBackground(new TextureRegionDrawable(MAP_TEXTURE_ATLAS.findRegion("map")));

// Scroll pane
ScrollPane scrollPane = new ScrollPane(container2);
container.setActor(scrollPane);

stage.addActor(container);

有没有办法将 WidgetGroup 与 ScrollPane 一起使用,或者我可以如何实现所需功能的任何方式。

谢谢

已接受答案的替代实现

阅读接受的答案后,我决定创建自己的类,实现如下;

// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Match3.getWidth(), Match3.getHeight());

class ScrollWidget extends WidgetGroup {

  private float prefHeight;
  private float prefWidth;

  public ScrollWidget(Image image) {

    prefHeight = image.getHeight();
    prefWidth = image.getWidth();

    addActor(image);
  }

  @Override
  public float getPrefHeight() {

    return prefHeight;
  }

  @Override
  public float getPrefWidth() {

    return prefWidth;
  }
}

ScrollWidget scrollWidget = new ScrollWidget(
    new Image(MAP_TEXTURE_ATLAS.findRegion("map"))
);

// Scroll pane
ScrollPane scrollPane = new ScrollPane(scrollWidget);
scrollPane.setOverscroll(false, false);

scrollPane.layout();
scrollPane.updateVisualScroll();
scrollPane.setScrollY(scrollPane.getMaxY());

container.setActor(scrollPane);

stage.addActor(container);

【问题讨论】:

    标签: libgdx widget scrollpane


    【解决方案1】:

    使用 WidgetGroup 时,您必须自己设置大小。 Image Actor 不知道首选的宽度/高度应该是多少,默认为 0。

    未经测试的代码,但我自己使用类似的东西:

    Stage stage = new Stage();
    WidgetGroup group = new WidgetGroup();
    Scrollpane scrollPane = new ScrollPane(group);
    scrollpane.setBounds(0,0,screenWidth,screenHeight);
    group.setBounds(0,0,totalWidth,totalHeight);
    Image image = new Image(texture);
    image.setBounds(0,0,imageWidth,imageHeight);
    group.addActor(image);
    stage.addActor(scrollPane);
    

    【讨论】:

    • 谢谢,这就是问题所在!我已经编辑了我的问题,以包含与您的解决方法类似的解决方案。
    猜你喜欢
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2015-08-14
    • 2015-02-16
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多