2014-10-31 217 views
2

我試圖在屏幕底部向我的遊戲添加一個較少的滑動菜單(左側和右側)。 我的問題是,我不能在屏幕的底部調整表,這是我的代碼:JAVA-LIBGDX如何正確設置scrollPane

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

}

這樣,我可以分割屏幕分爲2部分(頂部和底部),但輸入不正確:要激活第一個按鈕,我必須按屏幕的頂部,但底部是「設計」按鈕。

我怎樣才能創建一個在屏幕底部的滾動表?

回答

3

似乎視口,表的一些誤解,以及如何使用它們:

使用一個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對象,填充其父項填充整個屏幕。因此,在container.row().space(10).padBottom(10);之後將scroll添加到container之後,請確保將其他表添加到容器中。該表格將在滾動窗格下方繪製。

0

謝謝,我已經做到了,這是我的新代碼:

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