14

Android 和 libgdx 菜鸟在这里。

有人知道最近为 libgdx 发布的 UI API 吗?请参阅此处的博客文章:http ://www.badlogicgames.com/wordpress/?p=2058

我正在寻找创建一个基本的菜单系统,我想知道这个 UI API 是否会使它变得更容易。

4

2 回答 2

24

更新以反映对 LibGDX 的更改

我处于类似的位置,以下代码为我创建了一个基本菜单(按钮容器)。代码不会按原样工作,因为它使用了我的一些类,但真正重要的是 create 方法的内容。这会创建一个居中的标题,然后是容器中的一些按钮,这些按钮会居中,然后是左下角的 fps 标签和右下角的图像。主题文件和一些图像来自LibGDX 测试资产

我已经让它与 JOGL、LWJGL 和 android 应用程序类一起使用。我已经在 Droid 2 上运行它并让它像在我的桌面上一样运行。希望这可以帮助您入门。

public class MenuScreen extends Screen{
private Stage ui;
private Table window;
@Override
public void create(final Game game) {
    super.create(game);
    TextureRegion image = new TextureRegion(new Texture(Gdx.files.internal(Art.badlogicSmall)));
    Label fps = new Label("fps: ", Art.sSkin.getStyle(LabelStyle.class),"fps");
    ui = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), true);
    Gdx.input.setInputProcessor(ui);
    window = new Table("window");
    window.width = ui.width();
    window.height = ui.height();
    window.x = 0;
    window.y = 0;
    window.debug();
    Label title = new Label("Title",Art.sSkin.getStyle(LabelStyle.class),"title");
    Button newGame = new Button("New Game",Art.sSkin.getStyle(ButtonStyle.class),"new");
    newGame.setClickListener(new ClickListener() {
        @Override
        public void click(Actor actor) {
            game.setScreen(GameScreen.class);               
        }
    });
    Button optionMenu = new Button("Option",Art.sSkin.getStyle(ButtonStyle.class),"Options");
    Button helpMenu = new Button("Help",Art.sSkin.getStyle(ButtonStyle.class),"Help");
    Image libgdx = new Image("libgdx", image);
    window.row().fill(false,false).expand(true,false).padTop(50).padBottom(50);
    window.add(title);
    Table container = new Table("menu");
    container.row().fill(true, true).expand(true, true).pad(10, 0, 10, 0);
    container.add(newGame);
    container.row().fill(true, true).expand(true, true).pad(10, 0, 10, 0);
    container.add(optionMenu);
    container.row().fill(true, true).expand(true, true).pad(10, 0, 10, 0);
    container.add(helpMenu);
    window.row().fill(0.5f,1f).expand(true,false);
    window.add(container);
    Table extras = new Table("extras");
    extras.row().fill(false,false).expand(true,true);
    extras.add(fps).left().center().pad(0,25,25,0); 
    extras.add(libgdx).right().center().pad(0,0,25,25);
    window.row().fill(true,false).expand(true,true);
    window.add(extras).bottom();
    ui.addActor(window);
}

@Override
public void render(float arg0) {
    Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    ((Label)ui.findActor("fps")).setText("fps: " + Gdx.graphics.getFramesPerSecond());  
    ui.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
    ui.draw();
    Table.drawDebug(ui);
}
@Override
public void resize(int width, int height) {
    ui.setViewport(width, height, true);
    Log.d("Resize: "+width+", "+height);
}

于 2011-07-25T01:11:08.543 回答
7

是的,新的 UI api 非常好用。您可以使用Skin对象创建一些Actor对象,然后将它们加入 Stage对象。

您可以参考 libgdx 源中的 UITest.java 文件。它演示了如何使用基本的 UI 元素。

从底层看 libgdx 新 UI,它只包含以下元素:

  • NinePatch:创建元素的基本形状对象可以伸展;
  • Region:固定大小元素的形状对象;
  • 字体:显示文本的位图字体对象;

高级元素是由它们组成的,比如Button对象,包括:ninepatch和font ojbect等等。

因此,您可以非常轻松地使用它们创建 2D UI。:)

于 2011-09-03T06:01:58.850 回答