The Dialog class is a little unintuitive without looking closer.
This is why I provide a small working example:
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Dialog; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.utils.Timer; import com.badlogic.gdx.utils.Timer.Task; public class TestGame extends ApplicationAdapter { Dialog endDialog; Skin skin; Stage stage; @Override public void create() { skin = new Skin(Gdx.files.internal("uiskin.json")); stage = new Stage(); Gdx.input.setInputProcessor(stage); endDialog = new Dialog("End Game", skin) { protected void result(Object object) { System.out.println("Option: " + object); Timer.schedule(new Task() { @Override public void run() { endDialog.show(stage); } }, 1); }; }; endDialog.button("Option 1", 1L); endDialog.button("Option 2", 2L); Timer.schedule(new Task() { @Override public void run() { endDialog.show(stage); } }, 1); } @Override public void render() { Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); } @Override public void dispose() { stage.dispose(); } }
Each time you click on the "Dialog" parameter (option 1 or option 2), the result object is printed on system.out (here 1L or 2L, it can be any object), and the dialog is displayed again after 1 second.
uiskin is taken from libgdx tests.
To adapt it more to your needs, you can change the result method to:
protected void result(Object object) { if (object.equals(1L)) { gameScreen.setIntLives(3); gameScreen.setIntScore(0); System.out.println("Button Pressed"); } else {
And add the following buttons:
endDialog.button("Retry", 1L); endDialog.button("Main Menu", 2L);
Note that the Dialog class is created only once. (so my comment was not right)
Just to give you an idea of ββwhat you can do with it:
Since any object can be used, you can use reflection:
try { endDialog.button("doX", ClassReflection.getDeclaredMethod(this.getClass(), "doX")); endDialog.button("doY", ClassReflection.getDeclaredMethod(this.getClass(), "doY")); } catch (ReflectionException e) { // TODO Auto-generated catch block e.printStackTrace(); }
And the result will look like this:
protected void result(Object object) { try { ((Method) object).invoke(TestGame.this); } catch (ReflectionException e) {
Now you just need to implement these methods:
public void doX() { System.out.println("doX"); } public void doY() { System.out.println("doY"); }
Just to give you some ideas.