使用scene2d.ui与libgdx:哪里肌肤从何而来?而来、肌肤、scene2d、ui

由网友(半世流离唯独看不清你)分享简介:我读过有关libgdx的 scene2d功能,包括UI元素,但我不能让他们的工作。他们似乎都使用一个皮肤对象,我怎么创建?I've read about libgdx's scene2d features, including the UI elements, but I can't get them to work...

我读过有关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.

Atlaspack文件 Json文件 Atlaspack图片 Font文件 Atlaspack File Json File Atlaspack Image 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是所有的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.

阅读全文

相关推荐

最新文章