問(wèn)題描述
我已了解 libgdx 的 scene2d 功能,包括 UI 元素,但我不能讓他們工作.他們似乎都使用了一個(gè)皮膚對(duì)象,我該如何創(chuàng)建一個(gè)?
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?
我已經(jīng)完成了創(chuàng)建簡(jiǎn)單 libgdx 的 不錯(cuò)的教程在桶中捕捉雨滴的游戲,我通過(guò)加載帶有紋理圖集的圖像創(chuàng)建了一個(gè)簡(jiǎn)單的棋盤(pán)游戲應(yīng)用程序.但是,scene2d 聽(tīng)起來(lái)是一種更好的方式來(lái)控制棋盤(pán)游戲應(yīng)用程序中的移動(dòng)棋子以及任何按鈕和菜單.
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 測(cè)試項(xiàng)目中使用的示例皮膚文件開(kāi)始.
To load a skin, you can start with the sample skin files used in the libgdx test project.
- Atlaspack 文件
- Json 文件
- Atlaspack 圖片
- 字體文件
this question 中有更多細(xì)節(jié),我發(fā)現(xiàn)我必須將文件移動(dòng)到資產(chǎn)以避免 HTML 版本中的錯(cuò)誤.(如果我將文件留在 assets 文件夾中,我會(huì)看到一條錯(cuò)誤消息,例如GwtApplication: exception: Error reading file data/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.")
我發(fā)布了一個(gè)顯示單個(gè)按鈕的完整示例,并且有趣的代碼都在 游戲類(lèi).該示例非常簡(jiǎn)單,您只需要兩個(gè)成員變量來(lái)保存場(chǎng)景和按鈕.
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;
要?jiǎng)?chuàng)建皮膚,您只需加載數(shù)據(jù)文件.
To create the skin, you just load the data file.
Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
將按鈕連接到舞臺(tái).
stage = new Stage();
button = new TextButton("Click Me!", skin);
stage.addActor(button);
將監(jiān)聽(tīng)器連接到按鈕,并注冊(cè)舞臺(tái)以接收事件.
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);
render()
方法基本上是對(duì) stage.draw()
的調(diào)用.
The render()
method is basically a call to 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();
那么你只需要在調(diào)整屏幕大小時(shí)對(duì)屏幕進(jìn)行布局.
Then you just need to layout the screen when it resizes.
button.setPosition(
(width-button.getWidth())/2,
(height-button.getHeight())/2);
如果您的屏幕更復(fù)雜,請(qǐng)考慮使用 Table.
If your screen is more complicated, consider using a Table.
如果您想使用不同的字體,可以使用 生成字體文件和圖像文件希羅.完成后,您必須獲取新字體圖像并使用 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.
這篇關(guān)于將 scene2d.ui 與 libgdx 一起使用:皮膚從何而來(lái)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!