使用scene2d.ui与libgdx:哪里肌肤从何而来? [英] Using scene2d.ui with libgdx: where does the skin come from?

查看:480
本文介绍了使用scene2d.ui与libgdx:哪里肌肤从何而来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过有关libgdx的 scene2d功能,包括UI元素,但我不能让他们的工作。他们似乎都使用一个皮肤对象,我怎么创建?

I've read about libgdx's scene2d features, including the UI elements, but I can't get them to work. They all seem to use a skin object, how do I create one?

我通过很好的教程创建一个简单的libgdx游戏赶上雨滴不见了水桶,我已经通过加载图像与纹理地图中创建了一个简单的棋盘游戏的应用程序。然而,scene2d听起来像一个更好的方法来控制运动件的棋盘游戏的应用程序,以及任何按钮和菜单。

I've gone through the nice tutorial that creates a simple libgdx game catching raindrops in a bucket, and I've created a simple board game app by loading images with a texture atlas. However, scene2d sounds like a better way to control the moving pieces in a board game app, as well as any buttons and menus.

推荐答案

要加载的皮肤,你可以在libgdx测试项目中使用的样本皮肤文件启动。

To load a skin, you can start with the sample skin files used in the libgdx test project.

  1. <一个href="https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.atlas">Atlaspack文件
  2. <一个href="https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.json">Json文件
  3. <一个href="https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/uiskin.png">Atlaspack图片
  4. <一个href="https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests-android/assets/data/default.fnt">Font文件
  1. Atlaspack File
  2. Json File
  3. Atlaspack Image
  4. Font File

有一个在这个问题更详细一点,而且我发现我不得不将文件移动到一个子目录资产,以避免在HTML版本中的错误。 (如果我留在资产文件夹中的文件,我看到这样一个错误信息GwtApplication:异常:读取文件中的数据/ uiskin.json错误)

There's a bit more detail in this question, and I found that I had to move the files into a subdirectory of assets to avoid a bug in the HTML version. (If I leave files in the assets folder, I see an error message like, "GwtApplication: exception: Error reading file data/uiskin.json.")

我张贴了完整的例子显示一个按钮,有趣的code是所有的<一个href="https://github.com/donkirkby/vograbulary/blob/scene2d-demo/vograbulary/src/com/github/donkirkby/vograbulary/VograbularyGame.java">game类。这个例子非常简单,你只需要两个成员变量来保存现场和按钮。

I posted a complete example that displays a single button, and the interesting code is all in the game class. The example is so simple that you only need two member variables to hold the scene and the button.

private Stage stage;
private TextButton button;

要创建的皮肤,你只需加载数据文件。

To create the skin, you just load the data file.

Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));

线按钮进入阶段。

Wire the button into the stage.

stage = new Stage();
button = new TextButton("Click Me!", skin);
stage.addActor(button);

将一个监听到该按钮,并注册阶段接收事件。

Wire a listener into the button, and register the stage to receive events.

button.addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        button.setText("Clicked!");
    }
});
Gdx.input.setInputProcessor(stage);

渲染()方法基本上调用 stage.draw()

Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
stage.draw();

然后你只需要布局的屏幕时,它会调整。

Then you just need to layout the screen when it resizes.

button.setPosition(
        (width-button.getWidth())/2, 
        (height-button.getHeight())/2);

如果你的屏幕更加复杂,考虑使用

If your screen is more complicated, consider using a Table.

如果您要使用不同的字体,您可以使用 Hiero 生成的字体文件和图像文件。当你这样做,你就必须采取新的字体图像,并使用 TexturePacker 来与其他皮肤资产的重新包装。

If you want to use a different font, you can generate the font file and image file using Hiero. After you've done that, you'll have to take the new font image and use the TexturePacker to repack it with the other skin assets.

这篇关于使用scene2d.ui与libgdx:哪里肌肤从何而来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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