【问题标题】:Libgdx Missing LabelStyle font for ImageTextButtonLibgdx 缺少 ImageTextButton 的 LabelStyle 字体
【发布时间】:2017-03-10 20:04:42
【问题描述】:

我一直在使用 Button 类创建按钮,但我想我会尝试 ImageTextButton。但是,即使只有 Button 类,我也不太了解 .pack (atlas) 文件与 .json 文件如何协同工作的文档。

使用 Button 类,我可以在代码中“命名”任何我喜欢的按钮,并在 json 中相应地命名(参见下面的示例),但对于“图像”,我必须“命名”所有内容相同,从代码中的构造,到包和 json 文件。有效的示例:

代码:

// 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。在代码中,我根据玩家所在的位置动态构建它们:不同的商店有不同的物品要出售,这些物品被读入 items[] 数组。

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
...

【问题讨论】:

    标签: json button libgdx pack atlas


    【解决方案1】:

    您的 json 中的图像没有理由必须与其中的可绘制对象具有相同的名称。您甚至不需要将图像放入您的皮肤中。看documentation of ImageTextButtonStyle...它的参数是Drawables,不是Images。这就是为什么您似乎需要使用确切的纹理区域名称的原因。

    如果您尚未在 Skin 中创建纹理区域,Skin 会自动从具有该名称的纹理区域创建 TextureRegionDrawable 或 NinePatchDrawable。但是您可以创建各种 Drawable(例如 TiledDrawable 或 TintedDrawable,或带有自定义填充的 TextureRegionDrawable)并根据需要将它们指定为按钮的图像。

    还要在文档中注意 ImageTextButtonStyle 从 TextButtonStyle 继承了一个名为 font 的成员,该成员在文档中未标记为可选。因此,使用未指定字体的 ImageTextButtonStyle 会出现异常。

    有几种方法可以将字体放入皮肤中。如果您使用 BMFont 或 Heiro 等工具生成了位图字体,则可以将其打包到纹理图集中。只需将 .fnt 文件及其图像与您正在打包的其他图像放在同一目录中。并将 .fnt 文件的另一个副本放入您的资产目录中,在您的 atlas 文件旁边。然后使用它的文件名在你的皮肤 json 中指定字体:

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

    如果您使用的是 FreeTypeFontGenerator,这会更复杂。让我知道你是否正在这样做。

    【讨论】:

    • 我明白了,我没有在跟踪中返回足够多的内容来查看非可选字体的 TextButtonStyle 继承,感谢您指出这一点。查看我的旧项目,我实际上以您描述的两种方式制作字体,一种是在资产文件夹和 json 皮肤中生成字体和 .fnt 文件,然后是另一组使用 FreeTypeFontGenerator 的字体。我创建了一个“SmartFontGenerator”类来完成这项工作。我需要重新学习我在那里所做的事情(我制作那款游戏已经快一年了)。如果我走皮肤路线,我不应该在我的按钮创建中做更多的事情,对吗?
    • 你能澄清一下图像和json吗?所有定义的图像都用于背景,而不是按钮。我将图像添加到表格中,这些图像被添加到堆栈中,最后全部添加到舞台中。然后创建按钮并将其放置在顶部。因此,只需创建这些 Image 对象,我就必须将其命名为相同。
    • 如果您查看 ImageButtonStyle 或 ImageTextButtonStyle,请注意没有一个参数是图像。它们是可绘制的。我认为这就是导致您相信您必须将图像命名为与其包含的可绘制对象相同的名称的原因。实际上,您在 Json 中定义的那些图像将完全未使用。 (如果要检查,只需删除 Json 的 Image 部分。)当皮肤加载 Json 时,它正在寻找具有这些名称的 Drawable,当它没有找到某个名称的 Drawable 时,它​​会自动创建一个使用具有该名称的 TextureRegion。
    • 实例化Image时,传入的String参数不是Skin中Image的名称。这是一个 Drawable 的名称。同样的道理。没有理由在您的 Json 中定义图像,因为您将无法使用它们。
    • 知道了;我删除了 json 中的整个 Image 对象,并且能够很好地运行它。感谢您扩展 Drawables 的内容,我想我把它理顺了。
    猜你喜欢
    • 2016-07-12
    • 2017-05-07
    • 2019-04-15
    • 2010-09-24
    • 1970-01-01
    • 2014-04-12
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多