我读过 libgdx 的 scene2d features ,包括 UI 元素,但我无法让它们工作。他们似乎都使用皮肤对象,我该如何创建一个?
我已经完成了 nice tutorial这创建了一个简单的 libgdx 游戏来捕捉桶中的雨滴,我通过加载带有 texture atlas 的图像创建了一个简单的棋盘游戏应用程序.但是,scene2d 听起来像是一种更好的方式来控制棋盘游戏应用程序中的移动棋子,以及任何按钮和菜单。
最佳答案
要加载皮肤,您可以从 libgdx 测试项目中使用的示例皮肤文件开始。
this question 中有更多详细信息,我发现我必须将文件移动到 Assets 的子目录中以避免 HTML 版本中的错误。 (如果我将文件留在 Assets 文件夹中,我会看到一条错误消息,例如“GwtApplication:异常:读取文件数据/uiskin.json 时出错。”)
我发布了一个complete example显示单个按钮,有趣的代码都在 game class 中.这个例子非常简单,你只需要两个成员变量来保存场景和按钮。
private Stage stage;
private TextButton button;
要创建皮肤,您只需加载数据文件。
Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
将按钮连接到舞台上。
stage = new Stage();
button = new TextButton("Click Me!", skin);
stage.addActor(button);
将监听器连接到按钮,并注册阶段以接收事件。
button.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
button.setText("Clicked!");
}
});
Gdx.input.setInputProcessor(stage);
render()
方法基本上是对 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();
然后您只需要在屏幕调整大小时对其进行布局即可。
button.setPosition(
(width-button.getWidth())/2,
(height-button.getHeight())/2);
如果您的屏幕比较复杂,请考虑使用 Table .
如果你想使用不同的字体,你可以使用Hiero生成字体文件和图像文件。 .完成后,您必须获取新字体图像并使用 TexturePacker与另一个重新包装 skin assets .
关于java - 将 scene2d.ui 与 libgdx : where does the skin come from? 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18526116/