I'm developing a game where the player has an inventory, and you can find chests in the map with items which you can collect, so I created a class that extends from Dialog
to show both item lists, player's inventory and chest's inventory, just like this:
private class InventoryDialog extends Dialog{
public InventoryDialog(boolean isPlayerInventory, Inventory inventory) {
super(isPlayerInventory? "Inventory":"Chest", mySkin);
final List inventoryList = new List(mySkin);
inventoryList.setItems(inventory.getItems());
ScrollPane pane = new ScrollPane(inventoryList,mySkin);
getContentTable().add(pane).size(myDimensions);
button("Back");
if(isPlayerInventory){
button("Use").addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
player.getInventory().useItemAt(inventoryList.getSelectedIndex());
}});
}else{
button("Get").addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
player.getInventory().add(inventory.remove(inventoryList.getSelectedIndex()));
}});}
}
}
So they will use or get the item only if they click that button.
My problem is that the get/use listener is always invoked even if the clicked button is the "Back" button, what am I doing wrong? :/
button()
method defined? Can you provide the code? \$\endgroup\$button()
method for dialogs, I haven't created it, but I have already solved my problem, I will post the answer soon, so it doesn't matter. \$\endgroup\$