2016-06-09 130 views
0

大家好我正在嘗試使用Libgdx中的Table來顯示一些圖像,但它沒有正確顯示它們,我無法找到修復該問題的方法 當我使用table.bottom時,它會顯示它們,但當我使用table.center它顯示他們較低,當我選擇上,左或右它說明不了什麼 :這裏是我的代碼格式化表格:Libgdx表組件無法正確顯示

public class MenuIcons { 
    public Viewport viewport; 
    public Stage stage; 
    public boolean upPressed; 
    public boolean leftPressed; 
    public boolean rightPressed; 
    public boolean levellock1,levellock2; 
    public Image lock1; 
    public Image lock2; 

    public OrthographicCamera camera; 
    public Table table; 

    //Constructor. 
    public MenuIcons(SpriteBatch spriteBatch) { 
     camera = new OrthographicCamera(); 
     viewport = new FitViewport(400, 208, camera); 
     stage = new Stage(viewport, spriteBatch); 
     Gdx.input.setInputProcessor(stage); 
     initalizeTable(); 


    } 
    public void draw() { 
     stage.draw(); 
    } 

    public void resize(int width, int height) { 
     viewport.update(width, height); 
    } 
    public void initalizeTable() 
    { 
     //Buttons with images. 
     lock1 = new Image(new Texture("levellock.png")); 
     lock1.setSize(25, 25); 
     lock1.addListener(new InputListener() { 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       levellock1 = true; 
       return true; 
      } 

      @Override 
      public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
       levellock1 = false; 
      } 
     }); 
     lock2 = new Image(new Texture("levellock.png")); 
     lock2.setSize(25, 25); 
     lock2.addListener(new InputListener() { 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       levellock2 = true; 
       return true; 
      } 

      @Override 
      public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
       levellock2 = false; 
      } 
     }); 
     //Table with buttons. 
     table = new Table(); 
     table.bottom; //Align to the left bottom. 
     table.add(); 
     table.add(); 
     table.add(); 
     table.add(lock1).size(lock1.getWidth(), lock1.getHeight()); 
     table.add(lock2).size(lock2.getWidth(), lock2.getHeight()); 



     stage.addActor(table); 
    } 

    public boolean isLevellock1() { 
     return levellock1; 
    } 

    public boolean isLevellock2() { 
     return levellock2; 
    } 

} 

這裏是圖像,當我使用Table.bottom

這裏是圖像,當我使用Table.center

回答

1

我建議通過信息和例子在這裏閱讀: https://github.com/libgdx/libgdx/wiki/Table

此行甚至不會工作?:table.bottom;

試試這個table.left().bottom();而不是table.bottom;

或者更好的是你可以對齊單個項目,當你添加它們,這樣的事情:你爲什麼反覆使用空白table.add();

table.add(lock1).width(lock1.getWidth()).height(lock1.getHeight()).left().bottom();

注意事項?你不應該用table.row()代替嗎?


編輯發表評論:

你需要創建一個行或空間,爲您的項目,以適應,而不是使用table.add();。你正在造成各種各樣的問題。

在你走之前的任何進一步你只需要table = new Table();後添加此table.setDebug(true);所以你可以看到你在做什麼。

現在試試這個:

table = new Table(); 
    table.setDebug(true); 
    table.top().left(); 
    table.add(lock1).width(lock1.getWidth()).height(lock1.getHeight()).left().top(); 
    table.row(); 
    table.row(); 
    table.add(lock2).width(lock2.getWidth()).height(lock2.getHeight()).left().bottom(); 

現在你可以看到調試線,你可以看到你要去哪裏錯了。在table.row();之間添加一個空白標籤,並且使用.expand();使其填滿的空間是這樣的:在根表

table.row(); 
    table.add(myBlankLabel).expandY(); 
    table.row(); 
+0

我希望它是table.left()。但它並沒有工作。 我使用table.add在它們之間創建一些空格 –

+1

您確實需要閱讀我的消息開頭鏈接的文檔,因爲它具有所有答案。看看我的編輯:你需要添加這個'table.setDebug(true);'所以你可以看到發生了什麼。 – sorifiend

+0

謝謝,完成了 –

1

呼叫table.setFillParent(true)。否則,像table.bottom()這樣的調用就沒有意義,因爲邏輯表正在一個零大小的框中對齊。