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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.