【发布时间】: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()。它使用自己的批次。