我如何使用LibGDX使TextButtons? [英] How do I make TextButtons using LibGDX?

查看:154
本文介绍了我如何使用LibGDX使TextButtons?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经照着YouTube上的一些教程,使按钮我libgdx游戏,并已运行到哪里我button.pack无法加载的问题。

I've followed a few tutorials on youtube to make buttons for my libgdx game and have run into a problem where my button.pack cannot be loaded.

     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等)是在一个按钮文件夹,这是在我的资产文件夹下我的游戏 - GDX芯(见链接)。我不能编译,得到错误无法加载文件:按钮/ button.pack.png我不知道为什么它寻找一个PNG扩展时,我明明只是把buttons.pack,或者如果是连问题。请帮助!

all button files (buttonup.png, button.pack etc.) are in a 'buttons' folder which is in my assets folder under my-game-gdx-core (see link). I cannot compile and get the error 'Couldn't load file: buttons/button.pack.png' I don't know why it is looking for a png extension when i obviously just put buttons.pack, or if that is even the problem. Please help!

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

我不明白怎么可能出在该文件位于由于资产所有其它PNG的可加载的问题。

I don't see how it could be a problem with where the files are located because all other png's in assets can be loaded.

推荐答案

您最有可能得把你的图片在的Andr​​oid /资产文件夹中。要创建您的按钮的方式看起来不错但它LibGDX提供带提前一些工作按钮创建一个更方便的方法。这是我创造我的按钮:

You most probably have to put your images in the android/assets folder. The way you are creating your buttons looks fine however it LibGDX offers a much more convenient way of creating buttons with a bit of work in advance. This is how I create my buttons:

首先,我创建这样的一个形象:
href=\"http://i.stack.imgur.com/NRDkW.png\" rel=\"nofollow\">

First I create an images like these:

然后我收拾我的所有UI图像组合在一起使用 TexturePacker 。这生成一个图像薄片,并且看起来是这样的,但没有拆分一个txt文件参数:

I then pack all my UI images together using TexturePacker. This generates a image sheet and a txt file that looks something like this but without the split parameters:

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

分割参数的设计使这些图像正确舒展。 我曾经问一个问题上的英雄,使他们的工作方式。的那么什么你要做的就是查找图像中的绘图应用程序,看到部分需要什么在分割参数进行拉伸和填充。

The split parameters are designed to make those images stretch correctly. I once asked a question hero on SO how they work. So what you do is look up the image in your drawing app and see what portions need to be stretched and fill in the split parameters.

的精灵表和TXT文件一起被称为 TextureAtlas ,一旦你创建像 TextureAtlas地图集=新TextureAtlas一本地图册(uiskin。 PNG); 您可以通过要求纹理 atlas.findRegion(container_blue)然而,这是一个相当昂贵的调用,这样别做T这样每次更新。

The sprite sheet and txt file together are called a TextureAtlas and once you create an atlas like that TextureAtlas atlas = new TextureAtlas("uiskin.png"); you can ask for a texture by doing atlas.findRegion("container_blue") however this is a rather expensive call so don't do this each update.

现在对于真正的魔术LibGDX提供了皮肤,你可以存储样式。皮肤文件是以.json 格式如下:

Now for the real magic LibGDX provides a skin where you can store Styles. A skin file is in .json format and looks like this:

{
    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 }
    },
}

这其实很简单。首先,我找到我的字体,并命名它们,然后我加入一些颜色,我的字体。从这里我想补充我的标签两种风格和款式3我TextButtons。如果我这样加载到一个皮肤这样的护肤皮肤=新的皮肤(​​Gdx.files.internal(uiskin.json),阿特拉斯) ; 我可以创建我使用单线创建样式的按钮

It's actually very simple. First I locate my fonts and name them, then I add some colors for my fonts. From there I add two styles for my labels and 3 styles for my TextButtons. If I load this into a Skin like this Skin skin = new Skin("Gdx.files.internal("uiskin.json"), atlas); I can create many buttons with the styles I created using a single line.

//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 。这是更容易设定所有你需要的是一个很小的类。我使用的资产描述符,救了我,当我需要查找我的皮肤或地图集打字更加人物载入我的资产。

It's a bit more work in advance but this works very well. One more thing though, if you have many screens you should not be loading a new skin each time. You should use a AssetManager. This is even easier to setup all you need is a small class. I load my assets using a asset descriptor, saving me typing even more characters when I need to lookup my skin or atlas.

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<TextureAtlas> uiAtlas =
            new AssetDescriptor<TextureAtlas>("ui/uiskin.pack", TextureAtlas.class);
    //The skin
    public static final AssetDescriptor<Skin> uiSkin =
            new AssetDescriptor<Skin>("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加载酒吧,但基本上所有你需要做的是把下面为code的第一线运行。

You can use this using a splash screen and a fency loading bar but essentially all you need to do is putting the following as the first lines of code to run.

//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();

如果你释放你的游戏和加载时间超过几秒钟,你会好起来使用闪/加载屏幕somekind的。

If you are releasing you game and loading takes more then a couple of seconds then you will be better off using somekind of splash/loading screen.

这篇关于我如何使用LibGDX使TextButtons?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆