The problem I am having is that I find myself wanting to dynamically update my stages. For example, I have a stage that displays a list of saved "characters" (save files) that can be loaded. Each character profile has an "X" button next to it, to allow for the deletion of the profile. When this button is pressed, I want to delete the label representing the deleted profile, and update the stage accordingly. As you can see from the code below, I solve this by actually constructing an entirely new copy of the stage, which has only the correct labels (specifically, all the previous ones, minus the deleted one).
This is not an acceptable solution, because of the potentially heavy nature of constructing a new stage too often. It is fine for an event that will happen rarely, such as deleting a character. It won't work for a stage that needs to have its widgets updated possibly every frame. Is there a better way of creating and laying out stages such that they can be updated on the fly by the game logic?
- Code: Select all
private Stage loadMenu() {
Stage loadMenuStage = new Stage(viewport);
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
Table table = new Table();
table.setFillParent(true);
loadMenuStage.addActor(table);
for (String name : game.dm.loadNames()) {
table.add(loadName(name, skin)).width(700).height(75).fill().padRight(15);
table.add(deleteName(name, skin)).width(75).fill();
table.row().padTop(50);
}
table.row().padTop(80);
table.add(backToMain(skin)).width(200).height(50).bottom().center();
table.debug();
return loadMenuStage;
}
private TextButton loadName(String name, Skin skin) {
TextButton loadName = new TextButton(name, skin);
final String n = name;
loadName.addListener(new ChangeListener() {
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new CharacterScreen(game, game.dm.load(n)));
}
});
return loadName;
}
private TextButton deleteName(String name, Skin skin) {
TextButton deleteName = new TextButton("X", skin);
final String n = name;
deleteName.addListener(new ChangeListener() {
public void changed(ChangeEvent event, Actor actor) {
game.dm.delete(n);
setStage(loadMenu());
}
});
return deleteName;
}
private TextButton backToMain(Skin skin) {
TextButton backToMain = new TextButton("Main Menu", skin);
backToMain.addListener(new ChangeListener() {
public void changed(ChangeEvent event, Actor actor) {
setStage(mainMenu());
}
});
return backToMain;
}
public void setStage(Stage s) {
imux.removeProcessor(this.stage);
this.stage = s;
imux.addProcessor(this.stage);
}
