【问题标题】:How do I make TextButtons using LibGDX?如何使用 LibGDX 制作 TextButtons?
【发布时间】:2015-09-08 01:47:04
【问题描述】:

我已经按照 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 等)都位于 my-game-gdx-core 下的资产文件夹中的“按钮”文件夹中(参见链接)。我无法编译并得到错误“无法加载文件:buttons/button.pack.png”我不知道为什么当我显然只是放置buttons.pack时它正在寻找png扩展名,或者如果那甚至是问题。请帮忙!

这里是错误链接:https://gyazo.com/1ef8cce0b1935ad450d9f5229b0d698e

我看不出文件所在的位置有什么问题,因为可以加载资产中的所有其他 png。

【问题讨论】:

    标签: java button libgdx


    【解决方案1】:

    您很可能必须将图像放在android/assets 文件夹中。创建按钮的方式看起来不错,但是 LibGDX 提供了一种更方便的方式来创建按钮,需要提前做一些工作。这就是我创建按钮的方式:

    首先我创建一个像这样的图像:

    然后,我使用 TexturePacker 将所有 UI 图像打包在一起。这会生成一个图像表和一个 txt 文件,看起来像这样,但没有 split 参数:

    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
    

    分割参数旨在使这些图像正确拉伸。 I once asked a question hero on SO how they work. 所以你要做的就是在你的绘图应用中查找图像,看看哪些部分需要拉伸并填写分割参数。

    精灵表和 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 = new Skin("Gdx.files.internal("uiskin.json"), atlas); 这样的 Skin 中,我可以使用我使用单行创建的样式创建许多按钮。

    //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。这更容易设置,您只需要一个小班。我使用资产描述符加载我的资产,当我需要查找我的皮肤或图集时,我无需输入更多字符。

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

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

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

    如果您发布游戏并且加载时间超过几秒钟,那么您最好使用某种启动/加载屏幕。

    【讨论】:

      【解决方案2】:

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

          button.pack.png
      

      而不是

          button.png
      

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


      但是我发现还有一个问题——你使用了非常好的scene2d和非常好的skin实例——但是为什么你要创建skin >TextButtonStyle 从基础开始?

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-07
        • 1970-01-01
        • 2012-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多