2016-10-28 37 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