无法在libgdx游戏的菜单屏幕上绘制任何类型的按钮(android)

Jig*_*dya 5 android button libgdx

我想在我的游戏的主屏幕上显示文本按钮以进一步导航.但我无法这样做.我提到libgdx文档,但似乎没有任何工作.我有一个图像作为游戏背景,并希望在它上面显示按钮.

这就是我想要的:在此输入图像描述 下面是我的代码.亲切的帮助.谢谢.

public void create(){

            stage = new Stage();
            Gdx.input.setInputProcessor(stage);

            Skin skin = new Skin();
            skin.add("logo", new Texture("ic_launcher.png"));

            Pixmap pixmap = new Pixmap(1, 1, Format.RGBA8888);
            pixmap.setColor(Color.WHITE);
            pixmap.fill();
            skin.add("white", new Texture(pixmap));

            // Store the default libgdx font under the name "default".
            skin.add("default", new BitmapFont());

            // Configure a TextButtonStyle and name it "default". Skin resources are stored by type, so this doesn't overwrite the font.
            TextButtonStyle textButtonStyle = new TextButtonStyle();
            textButtonStyle.up = skin.newDrawable("white", Color.DARK_GRAY);
            textButtonStyle.down = skin.newDrawable("white", Color.DARK_GRAY);
            textButtonStyle.checked = skin.newDrawable("white", Color.BLUE);
            textButtonStyle.over = skin.newDrawable("white", Color.LIGHT_GRAY);
            textButtonStyle.font = skin.getFont("default");
            skin.add("default", textButtonStyle);

            // Create a table that fills the screen. Everything else will go inside this table.
            Table table = new Table();
            table.setFillParent(true);
            stage.addActor(table);

            // Create a button with the "default" TextButtonStyle. A 3rd parameter can be used to specify a name other than "default".
            final TextButton button = new TextButton("Click me!", skin);
            table.add(button);

            table.add(new Image(skin.newDrawable("white", Color.RED))).size(64);

} 

  public void render(float delta) {
        /*Gdx.gl.glClearColor(0,0,1.0f,1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);*/

        camera.update();
        game.batch.setProjectionMatrix(camera.combined);

        game.batch.begin();
        game.batch.draw(splsh, 0, 0,800,500);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
        Table.drawDebug(stage);
        game.batch.end();

        if (Gdx.input.isTouched()) {
            game.setScreen(new GameScreen(game));
            dispose();
        }
    }
Run Code Online (Sandbox Code Playgroud)

截至目前,我只想要按钮,文本按钮或图像按钮.

Met*_*ore 2

你的代码render()看起来很奇怪。

首先单独调用您自己的game.batch方法和阶段方法。那么你的render()方法应该如下所示:

public void render(float delta) {
    Gdx.gl.glClearColor(0,0,1.0f,1); // You should call this method only once outside of draw()
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    camera.update();
    game.batch.setProjectionMatrix(camera.combined);
    game.batch.begin();
    game.batch.draw(splsh, 0, 0,800,500);
    /* 
     * Render here everything that uses your camera and batch,
     * and is not a part of your stage.
     */
    game.batch.end();

    // And here is stage acts...
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
    Table.drawDebug(stage);

    if (Gdx.input.isTouched()) {
        game.setScreen(new GameScreen(game));
        dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您应该看到所有的舞台演员。

顺便说一下,默认情况下阶段管理它自己的相机和批次,所以你不必做这样的事情。

希望这会有所帮助,祝你好运。