2016-10-28 38 views
0

我一直在創建Button(s)只是使用Button類,但想通了我會嘗試ImageTextButton。然而,即使只有Button類,我也不太關注帶有.json文件的.pack(atlas)文件應該如何協同工作的文檔。使用Button類,我可以'命名'我的按鈕,無論我喜歡在代碼中,並相應地在json中命名(參見下面的示例),但對於'圖片',我必須'命名'一切都一樣,從代碼構建,通過包和json文件。什麼樣的作品舉例:Libgdx Missing LabelStyle字體的ImageTextButton

代碼:

// note the 'Constants' just point to the json and atlas files, respectively. 
skinShops = new Skin(Gdx.files.internal(Constants.SKIN_SHOPS), new 
TextureAtlas(Constants.TEXTURE_ATLAS_SHOPS)); 
// for this next line, note the json definition for Button$ButtonStyle: 
Button buttonBuy = new Button(skinShops, "Button-Buy"); 
// for Images, it seems I cannot have a 'unique' name first, then the drawable name, which I thought refers to the json 
Image imgItemsBackground = new Image(skinShops, "shop-items-background"); 

如果你看一下 'scene2d.ui.Image:' 定義,一切都被命名爲相同。雖然它的工作方式,我不清楚爲什麼。

這可能是2個問題,我猜,以前關於所有這些元素之間的命名命名的問題,但我目前的問題,我不能過去試圖構建一個ImageTextButton。在代碼中,我根據玩家的位置動態構建它們:不同的商店有不同的商品可供銷售,並且這些商品被讀入商品[]數組中。

ImageTextButton buttonPurchase = new ImageTextButton(items[j], skinShops, "shop_item-button-background"); 

堆棧跟蹤:

Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: Missing LabelStyle font. 
at com.badlogic.gdx.scenes.scene2d.ui.Label.setStyle(Label.java:78) 
at com.badlogic.gdx.scenes.scene2d.ui.Label.<init>(Label.java:72) 
at com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton.<init>(ImageTextButton.java:57) 
at com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton.<init>(ImageTextButton.java:44) 
at com.uv.screen.InteriorScreen.buildItemButtons(InteriorScreen.java:134) 

應該在哪裏我可以創建/設置這些按鈕的字體?

JSON文件:

{ 
com.badlogic.gdx.scenes.scene2d.ui.Button$ButtonStyle: { 
Button-Exit: { down: Button_Exit_112x60, up: Button_Exit_112x60 }, 
Button-Buy: { down: Button_Buy_112x60, up: Button_Buy_112x60 }, 
Button-Sell: { down: Button_Sell_112x60, up: Button_Sell_112x60 } 
}, 
com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton$ImageTextButtonStyle: { 
    shop_item-button-background: { down: shop_item-button-background, up: shop_item-button-background } 
}, 
com..badlogic.gdx.scenes.scene2d.ui.Image: { 
    background: { drawable: background }, 
    shop-items-background: { drawable: shop-items-background }, 
    shop-menu-background: { drawable: shop-menu-background }, 
    shop-talk-text-background: { drawable: shop-talk-text-background }, 
    weapon-shop-portrait_world1: { drawable: weapon-shop-portrait_world1 }, 
    armor-shop-portrait_world1: { drawable: armor-shop-portrait_world1 }, 
    item-shop-portrait_world1: { drawable: item-shop-portrait_world1 }, 
    inn-shop-portrait_world1: { drawable: inn-shop-portrait_world1 } 
} 

地圖集文件:

shops-pack.png 
format: RGBA8888 
filter: Nearest,Nearest 
repeat: none 
background 
    rotate: false 
    xy: 0, 504 
    size: 800, 480 
    orig: 800, 480 
    offset: 0, 0 
    index: -1 
shop-items-background 
    rotate: false 
    xy: 0, 248 
    size: 608, 256 
    orig: 608, 256 
    offset: 0, 0 
    index: -1 
shop_item-button-background 
    rotate: false 
    xy: 0, 60 
    size: 256, 60 
    orig: 256, 60 
    offset: 0, 0 
    index: -1 
... 

回答

1

沒有理由你在你的JSON圖像必須具有相同的名稱作爲其中的可繪製。您甚至不需要將圖像放入皮膚。看看documentation of ImageTextButtonStyle ......它的參數是可繪製的,而不是圖像。這就是爲什麼它看起來像你需要使用確切的紋理區域名稱。

如果您尚未在皮膚中創建紋理區域,Skin會自動使用該名稱在紋理區域外創建TextureRegionDrawable或NinePatchDrawable。但是,您可以創建各種Drawable(如TiledDrawable或TintedDrawable,或帶有自定義填充的TextureRegionDrawable),並將它們指定爲按鈕的圖像(如果需要)。

另請注意,在文檔中ImageTextButtonStyle從TextButtonStyle繼承了名爲font的成員,該成員在文檔中未標記爲可選。所以你會得到一個使用未指定字體的ImageTextButtonStyle的異常。

有幾種方法可以將字體放入皮膚。如果您使用BMFont或Heiro等工具生成了位圖字體,則可以將其包裝到紋理圖集中。只需將.fnt文件及其圖像放入與您打包的其他圖像相同的目錄中即可。並將.fnt文件的另一個副本放到您的資產目錄中,放在您的地圖集文件旁邊。然後,通過使用它的文件名指定你的皮膚JSON字體:

com.badlogic.gdx.graphics.g2d.BitmapFont: { myfont: { file: myfont.fnt } }, 

如果您正在使用FreeTypeFontGenerator,這是比較複雜的。讓我知道,如果這就是你在做什麼。

+0

我明白了,我沒有足夠的回溯來查看TextButtonStyle字體的繼承性,這是非可選的,謝謝指出。 看着我的老項目,我實際上是用兩種方法描述字體,一種是在assets文件夾和json皮膚中生成字體和.fnt文件,然後是使用FreeTypeFontGenerator的另一組字體。我創建了一個'SmartFontGenerator'類來完成這項工作。我需要重新學習我在那裏做了什麼(自從我做那場比賽以來,差不多一年)。 如果我去皮膚路線,我不應該做任何更多的按鈕創建,對不對? –

+0

你能澄清一下Image和json嗎?所有圖像的定義都用於背景,而不是按鈕。我將這些圖像添加到表格中,並將其添加到堆棧中,最後將所有圖片添加到舞臺上。然後按鈕被創建並放置在頂部。所以只需創建這些Image對象,我不得不將其命名。 –

+0

如果您查看ImageButtonStyle或ImageTextButtonStyle,請注意,沒有任何參數是圖像。他們是Drawables。我認爲這就是讓你相信你必須爲你的圖片命名與其所包含的繪圖相同的名稱。實際上,你在Json中定義的圖像完全沒有使用。 (如果你想檢查一下,只需刪除Json的圖像部分。)當皮膚加載Json時,它正在尋找具有這些名稱的Drawable,並且當它沒有通過某個名稱找到Drawable時,它會自動創建一個使用具有該名稱的TextureRegion。 – Tenfour04