The Dialog class is a little unintuitive without taking a closer look.
That's 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 an Dialog option (option 1 or option 2) the result object is printed on the system.out (here 1L or 2L, can be any object) and the dialog is displayed again in 1 second.
The uiskin is taken from the 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 {
// Goto main menut
}
};
And add the buttons like this:
endDialog.button("Retry", 1L);
endDialog.button("Main Menu", 2L);
Note that the Dialog class is only instantiated once. (so my comment was not correct)
Just to give you an idea on what you can do with it:
Since any object can be used you could 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 method would look like:
protected void result(Object object)
{
try
{
((Method) object).invoke(TestGame.this);
} catch (ReflectionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
};
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 a few ideas.