如何使用LibGDX制作TextButtons?

我在youtube上按照了一些教程为我的libgdx游戏制作按钮,并遇到了无法加载我的button.pack的问题。

stage = new Stage(); black = new BitmapFont(Gdx.files.internal("font/black.fnt")); white = new BitmapFont(Gdx.files.internal("font/white.fnt")); atlas = new TextureAtlas(Gdx.files.internal("buttons/button.pack")); skin = new Skin(atlas); table = new Table(skin); table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); TextButtonStyle textButtonStyle = new TextButtonStyle(); textButtonStyle.up = skin.getDrawable("buttons/buttonup.png"); textButtonStyle.over = skin.getDrawable("buttons/buttonpressed.png"); textButtonStyle.down = skin.getDrawable("buttons/buttonpressed.png"); textButtonStyle.pressedOffsetX = 1; textButtonStyle.pressedOffsetY = -1; textButtonStyle.font = black; buttonPlay = new TextButton("PLAY", textButtonStyle); buttonPlay.pad(20); table.add(buttonPlay); buttonHelp = new TextButton("HELP", textButtonStyle); buttonHelp.pad(20); table.add(buttonHelp); buttonExit = new TextButton("EXIT", textButtonStyle); buttonExit.pad(20); table.add(buttonExit); table.debug(); stage.addActor(table); 

所有按钮文件(buttonup.png,button.pack等)都在’buttons’文件夹中,该文件夹位于my-game-gdx-core下的我的资源文件夹中(参见链接)。 我无法编译并得到错误’无法加载文件:buttons / button.pack.png’我不知道为什么它正在寻找一个png扩展,当我显然只是把buttons.pack,或者如果这甚至是问题。 请帮忙!

以下是错误的链接: https : //gyazo.com/1ef8cce0b1935ad450d9f5229b0d698e

我不知道文件位于何处可能会出现问题,因为可以加载资源中的所有其他png。

你很可能不得不把你的图像放在android/assets文件夹中。 你创建按钮的方式看起来不错,但LibGDX提供了一种更方便的方法来提前创建按钮。 这是我创建按钮的方式:

首先,我创建这样的图像: 在此处输入图像描述 在此处输入图像描述

然后我使用TexturePacker将所有UI图像打包在一起。 这会生成一个图像表和一个类似于此但没有split参数的txt文件:

 uiskin.png format: RGBA8888 filter: Linear,Linear repeat: none container_blue rotate: false xy: 17, 2 size: 41, 45 split: 20, 20, 22, 22 orig: 41, 45 offset: 0, 0 index: -1 container_gold rotate: false xy: 60, 2 size: 41, 45 split: 20, 20, 22, 22 orig: 41, 45 offset: 0, 0 index: -1 

分割参数旨在使这些图像正确拉伸。 我曾经问过一个问题英雄,他们是如何工作的。 因此,您要做的是在绘图应用程序中查找图像,查看需要拉伸的部分并填写拆分参数。

精灵表和txt文件一起称为TextureAtlas ,一旦你创建了一个类似于TextureAtlas atlas = new TextureAtlas("uiskin.png"); 您可以通过执行atlas.findRegion("container_blue")来请求纹理,但这是一个相当昂贵的调用,所以不要每次更新都这样做。

现在,真正的魔力LibGDX提供了一个可以存储样式的皮肤。 皮肤文件采用.json格式,如下所示:

 { com.badlogic.gdx.graphics.g2d.BitmapFont: { default: { file: fonts/myDefaultFont.fnt } large: { file: fonts/myLargeFont.fnt } }, com.badlogic.gdx.graphics.Color: { white: { a: 1, r: 1, g: 1, b: 1 } green: { a: 1, r: 0, g: 1, b: 0 } gray: { a: 1, r: 0.5, g: 0.5, b: 0.5 } black: { a: 1, r: 0, g:0, b:0 } }, com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: { default: { font: default, fontColor: white } container_gold: { background: container_gold, font: default, fontColor: green } }, com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: { default: { font: large, fontColor: white } container_gold: { down: container_gold, up: container_blue, disabled: container_grey, font: large, fontColor: green } container_gold_small: { down: container_gold, up: container_blue, disabled: container_grey, font: default, fontColor: green } }, } 

它实际上非常简单。 首先,我找到我的字体并命名它们,然后我为我的字体添加一些颜色。 从那里我为我的标签添加了两种样式,为我的TextButtons添加了3种样式。 如果我将它加载到Skin就像这样Skin skin = new Skin("Gdx.files.internal("uiskin.json"), atlas);我可以创建许多按钮,其中包含我使用单行创建的样式。

 //This would create a "larger button using the default font. TextButton textButton = new TextButton("Click Me!", skin, "container_gold"); //This would create a smaller button TextButton textButton = new TextButton("Click Me!", skin, "container_gold_small"); //This would create a label looking exactly like the first button Label label = new Label("Label", skin, "container_gold"); //This would draw smaller white text without a background using the default style Label label = new Label("Default text", skin); 

提前做了一些工作,但效果很好。 还有一件事,如果你有很多屏幕,你不应该每次都加载一个新的皮肤。 您应该使用AssetManager 。 这更容易设置你需要的只是一个小class级。 我使用资产描述符加载我的资产,当我需要查找我的皮肤或地图集时,我可以输入更多字符。

 public class Assets { //The asset manager public static AssetManager manager = new AssetManager(); //The atlas, I renamed .txt to pack (just a habit). public static final AssetDescriptor uiAtlas = new AssetDescriptor("ui/uiskin.pack", TextureAtlas.class); //The skin public static final AssetDescriptor uiSkin = new AssetDescriptor("ui/uiskin.json", Skin.class, new SkinLoader.SkinParameter("ui/uiskin.pack")); //Method for loading the assets into the manager public static void load() { manager.load(uiAtlas); manager.load(uiSkin); } //Easy asset disposing, whenever you are done with it just dispose the manager instead of many files. public void dispose() { manager.dispose(); } } 

您可以使用启动画面和fency加载栏来使用它,但基本上您需要做的就是将以下内容作为要运行的第一行代码。

 //This is on top of my first `create()` method. //Tell the manager to start loading Assets.load(); //Tell the program to "loop" the loading until finished. Essentially stopping the game from continuing. Assets.manager.finishLoading(); 

如果您正在释放游戏并且加载需要花费几秒钟的时间,那么您最好使用某些启动/加载屏幕。

.pack文件的第一行是指连接到atlas的位图。 我很确定那里有什么

  button.pack.png 

代替

  button.png 

只需编辑它,这个应该没问题。


但是我看到还有一个问题 – 你使用的scene2d非常好, 皮肤实例也非常好 – 但是为什么要从基础创建TextButtonStyle

在我看来,你应该使用皮肤机制创建你的TextButtonStyle 。 您可以在这里阅读它并查看一些示例(即使使用TextButton):

http://www.gamefromscratch.com/post/2013/12/18/LibGDX-Tutorial-9-Scene2D-Part-3-UI-Skins.aspx