2016-03-29 94 views
2

根据我的理解,ScrollPane可以通过使用Table作为ScrollPane小部件(这是在libGDX repo中的ScrollPane测试中如何完成的)来添加多个小部件。libGDX ScrollPane使用WidgetGroup而不是Table作为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); 

根本没有显示在屏幕上;

但它确实与下面的代码工作(这显然不是我想要为滚动窗格窗口小部件是可以有只有一个演员集装箱)

// 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); 

回答

0

使用WidgetGroup时,您必须自己设置尺寸。该图片演员没有什么首选宽度想法/高度应该和将默认为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); 
+0

谢谢,这是问题!我编辑了我的问题,以包含类似于您解决问题的解决方案。 – GradeRetro