2017-05-22 18 views
1

我已經花了幾個小時試圖解決這個問題,並且我瘋了,請看看我的問題。當setFillParent爲true時,演員的位置是錯誤的,在LibGDX

圖片示出了此問題:

Image illustrating this

  • exitButtonsetFillParent功能被設定爲false的 結果是圖像的右邊,按鈕居中 內的Container但大小是錯誤的。

  • exitButtonsetFillParent功能被設置爲所述true結果 是圖像的左側,按鈕的尺寸正確地 但位置是錯誤的。

下面的代碼:

Container container = new Container(exitButton); 

    container.fill(); 

    container.setRound(false); 

    container.setSize(buttonSize, buttonSize); 

    exitButton.setFillParent(true); 

    //exitButton.setFillParent(false); 

    container.setPosition(
      ((float)viewportWidth/7f) - (buttonSize/2f) 
      ,((float)viewportHeight*5f/6f) - (buttonSize/2f) 
    ); 
+0

什麼有關[這](https://stackoverflow.com/questions/43958162/having-problems-with-the-layout-widget-container-in-libgdx)的問題,尋找類似 – Aryan

+0

使用'scale'而不是'resize'也可以對容器進行轉換。 – Aryan

回答

0

Widget是設計成由他們的父母來調整大小。 setFillParent(true)允許小部件覆蓋此並設置其自己的大小(以匹配其父大小)。除非它是層次結構的頂層小部件,否則您從不想這樣做,因爲當小部件控制了它自己的大小時,它的父節點失去了正確定位它的能力。

至於爲什麼按鈕被繪製得比容器大,如果兒童的minWidth/minHeight比父母大。假設exitButton是一個ImageButton或只是一個Button,這意味着它的圖像或背景的drawable大於buttonSize

最簡單的方法是覆蓋getMinWidth()getMinHeight()以返回零。例如:

exitButton = new Button(skin, style){ 
    public float getMinWidth(){return 0;} 
    public float getMinHeight(){return 0;} 
};