0
\$\begingroup\$

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? :/

\$\endgroup\$
5
  • \$\begingroup\$ How's your button() method defined? Can you provide the code? \$\endgroup\$
    – Neerkoli
    Commented Aug 3, 2016 at 10:45
  • \$\begingroup\$ @WeirdElfB0y It's the 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\$
    – Alex Pi
    Commented Aug 5, 2016 at 21:24
  • \$\begingroup\$ @AlexPi Did you solve the issue? If so, could you post it as an answer and accept that? \$\endgroup\$
    – user35344
    Commented Sep 16, 2016 at 6:22
  • 1
    \$\begingroup\$ @Tyyppi_77 Done. \$\endgroup\$
    – Alex Pi
    Commented Sep 18, 2016 at 17:47
  • \$\begingroup\$ Nice, it's awesome to actually see people return with answers! \$\endgroup\$
    – user35344
    Commented Sep 18, 2016 at 18:42

2 Answers 2

2
\$\begingroup\$

The problem is, that I should not add a listener when I'm using the "button()" method, the correct way to use this method is, simply add the button, and as a second argument, you pass an object to recognize your button, once in the result method, you use an if to compare the object passed to know which button you clicked, 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","use");       
    else button("Get","get");

}

@Override
    protected void result(Object object) {
        super.result(object);
        if(object.equals("use")){
           player.getInventory().useItemAt(inventoryList.getSelectedIndex());
        }else if(object.equals("get")
           player.getInventory().add(inventory.remove(inventoryList.getSelectedIndex()));
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

Im not sure, but I think it's because of the different Positions. There different Positions :

  1. Screen Position
  2. World Position

Those are different so your Button is on Screen Position x = 500, y = 500 but in the game on a totally different one.

Im not a well explainer so here's a link :

Convert Screen coords to World Coords LIBGDX

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .