【问题标题】:Unable to draw any sort of button on Menu Screen of libgdx game(android)无法在 libgdx 游戏的菜单屏幕上绘制任何类型的按钮(android)
【发布时间】:2014-02-04 12:38:44
【问题描述】:

我想在我的游戏主屏幕上显示文本按钮以进一步导航。但我无法这样做。我参考了 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();
        }
    }

到目前为止,我只想要按钮、文本按钮或图像按钮。

【问题讨论】:

  • 请显示你的 render() 方法。
  • @noone : 请检查我的更新代码 :)
  • 在将表格添加到舞台之前尝试添加按钮。也许这有帮助?
  • @Springrbua :感谢您查看代码。但没有变化..
  • 尝试将stage.draw()移出batch.begin()/batch.end()。它使用自己的批次。

标签: android button libgdx


【解决方案1】:

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

您自己的game.batch 和stage 方法的所有单独调用中的第一个。 那么你的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();
    }
}

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

顺便说一句,默认情况下舞台管理它自己的相机和批处理,所以你不必做这些事情。

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

【讨论】:

    猜你喜欢
    • 2014-10-21
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多