【问题标题】:JAVA-LIBGDX How to set up correctly a scrollPaneJAVA-LIBGDX 如何正确设置滚动窗格
【发布时间】:2014-10-31 17:10:59
【问题描述】:

我正在尝试在我的游戏中在屏幕底部添加一个滑动较少的菜单(左右)。 我的问题是我无法调整屏幕底部的表格,这是我的代码:

   private void initUI() {
  // inizializzazione dello stage
  stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()/4));
  Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
  Gdx.input.setInputProcessor(stage);

  // inizializzazione della tabella 
  container = new Table();
  container.setFillParent(true);
  stage.addActor(container);

  Table table = new Table();
  table.debug();

  final ScrollPane scroll = new ScrollPane(table, skin);
  scroll.setFillParent(true);

  InputListener stopTouchDown = new InputListener() {
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        event.stop();
        return false;
     }         
  };

  table.pad(20).defaults().expandY().space(5);

  for (int i = 0; i < 15; i++) {

     table.row();
     TextButton button = new TextButton(i + "dos", skin);
     table.add(button);
     button.addListener(new ClickListener() {
        public void clicked (InputEvent event, float x, float y) {
           System.out.println("click " + x + ", " + y);
        }
     });
  }      

  container.add(scroll).expandY().fill().colspan(1);
  container.row().space(10).padBottom(10);

}

render(){
  Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight()/4); 
  Gdx.input.setInputProcessor(stage);
  stage.draw();
  stage.act(Gdx.graphics.getDeltaTime());
  Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

}

通过这种方式,我可以将屏幕分成两部分(顶部和底部),但输入不正确:要激活第一个按钮,我必须按下屏幕顶部,但按钮是“设计”在底部。

如何在屏幕底部创建一个带有滚动窗格的表格?

【问题讨论】:

    标签: java user-interface libgdx scrollpane


    【解决方案1】:

    对视口、表格以及如何使用它们似乎有些误解:

    使用一个Viewport 来渲染整个屏幕。使用Table 拆分屏幕。因此将initUI 中的第一行更改为

    stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()));(去掉 /4)

    并将您的渲染方法调整为:

     @Override
     public void render(float delta) {
            Gdx.gl.glClearColor(0, 0, 0, 0);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.
            stage.act(delta);
            stage.draw();
     }
    

    清除您的颜色缓冲区,以确保正确绘制动画或按钮操作。不要将stage 设置为InputProcessor 每一帧调用。在initUI 中完成此操作后就可以了。

    到表格的大小:我假设表格与包含的元素不匹配。因此,每次使用table.add(button) 添加按钮时,都会使用table.add(button).height(...).width(...) 添加有关其高度和宽度的信息。然后向右调整表格和相应的滚动窗格。

    目前我只看到container 包含填充其父对象的scroll 对象,该对象填充了整个屏幕。因此,在将scroll 添加到container 之后,请确保在容器中添加另一个表container.row().space(10).padBottom(10);。此表格将绘制在滚动窗格下方。

    【讨论】:

      【解决方案2】:

      谢谢,我已经完成了,这是我的新代码:

          // inizializzazione dello stage
          stage = new Stage(new ExtendViewport(Constants.VIEWPORT_GUI_WIDTH,Constants.VIEWPORT_GUI_HEIGHT/4));
          Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
          Gdx.input.setInputProcessor(stage);
      
          // inizializzazione della tabella 
          container = new Table();
          container.setFillParent(true);
          container.bottom();
          stage.addActor(container);
      
          Table table = new Table();
          table.debug();
          table.bottom();
      
          final ScrollPane scroll = new ScrollPane(table, skin);
          //scroll.setFillParent(true);
          scroll.setupFadeScrollBars(0f, 0f);
      
          InputListener stopTouchDown = new InputListener() {
              public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                  event.stop();
                  return false;
              }           
          };
      
          table.pad(20).defaults().space(5);
      
          for (int i = 0; i < 15; i++) {
              TextButton button = new TextButton(i + "dos", skin);
              table.add(button).height(scroll.getHeight()).width(Constants.VIEWPORT_GUI_WIDTH/8);
              button.addListener(new ClickListener() {
                  public void clicked (InputEvent event, float x, float y) {
                      System.out.println("click " + x + ", " + y);
                  }
              });
          }       
          container.bottom();
          container.add(scroll).height(Gdx.graphics.getHeight()/3);//.expandY().fill().colspan(1);
          container.row().space(10).padBottom(10);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        • 1970-01-01
        • 2012-12-10
        • 2016-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多