我想在我的游戏主屏幕上显示文本按钮以进一步导航。但我无法这样做。我提到了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();
}
}
截至目前,我只想要按钮,文字按钮或图像按钮。
答案 0 :(得分: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();
}
}
现在您应该看到所有舞台演员。
顺便说一下,默认情况下管理它自己的相机和批次,所以你不必做这些事情。
希望这会有所帮助,祝你好运。