【发布时间】: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