I want to display Text Buttons on main screen of my game to navigate further.But I'm unable to do so.I referred libgdx documentation but nothing seems working.I have an Image as a Game background and want to display button over it.
Here is what I want : Below is my code.Kindly help.Thanks.
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();
}
}
As of now I just want either button,text button or Image button.
Your code in render()
looks very strange.
The first of all separate calls of your own game.batch
and stage methods.
Then your render()
method should look so:
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();
}
}
Now you should see all of your stage actors.
By the way, by default stage manage it's own camera and batch, so you don't have to do such things.
Hope this will helpful, good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With