0

I'm not sure if this is an issue with my for loop, or the rectangle position, but I have set a breakpoint and used a debug draw to draw the rectangles and they appear to be correct.

public void onAction(String name, boolean value, float tpf) {
     if(name.equals("ShowInventory")){
        if(!value){
            if(Statics.s_ShowInventory == true){
                Statics.s_ShowInventory = false;
            }else{
                Statics.s_ShowInventory = true;
            }
        }
    }

     if(name.equals("RightClick") && Statics.s_ShowInventory == true){
         Vector2f mousePos = screen.getMouseXY();
        for(int i = 0; i < 40; i++)
        {
            if(Main.inventory.inventorySlots[i].rect.Contains(mousePos)){
                System.out.println(Main.inventory.inventorySlots[i].slotNumber);
            }
        }
     }
}

What is happening is that the only rectangle that is writing to the console is the very first one. What I want to happen is every time a right click is true and a Boolean is true, loop through the list of rectangles and find out which one contains the mousePos. When the rectangle is clicked, only "0" appears in the console so I know I am not getting any other inventory slot number overlap.

Another thing that may be happening is that the loop may not be running fully, which would be a problem in my click method.

   public float x;
public float y;
public float width;
public float height;

public Rect(float x, float y, float width, float height){
    this.x = x; 
    this.y = y;
    this.width = width;
    this.height = height;
}

public boolean Contains(Vector2f pos){
    if(pos.x > x && pos.x < width && pos.y < height && pos.y > y){
        return true;
    }
    else
        return false;
}


           Element e = createInventorySlot(i, x, y);
            inventoryElements[i] = e;
            inventorySlots[i] = new InventorySlot(i, 0, "empty", new Rect(inventoryElements[i].getPosition().x, inventoryElements[i].getPosition().y, iconSize, iconSize));
            e.setToolTipText(inventorySlots[i].itemName + " : " + inventorySlots[i].slotNumber + " : " + inventorySlots[i].quantity);
    inventory.addChild(e);
  • It's not quite clear what you're asking - to me it looks like if these inventory slots are not overlapping then you should only expect to see one printed. – argentage Aug 22 '15 at 02:30
  • I am asking if there is anything that stands out which would give me the problem of only the element[0] being read at the click? If I click any other inventorySlot, nothing is shown in the Console. – huehuehuehuehue Aug 22 '15 at 02:37
  • What should I be using instead? I have the Vector2 mouse position and an array of rectangles to check which rectangle contains the mouse. – huehuehuehuehue Aug 22 '15 at 02:41
  • Use a Mouse Listener. Is this a Swing GUI? Show more and tell more. – Hovercraft Full Of Eels Aug 22 '15 at 02:41

1 Answers1

1

The for loop is looping all the way through. Try printing out the status of the if statement each loop.

for(int i = 0; i < 40; i++)
    {
    //    if(Main.inventory.inventorySlots[i].rect.Contains(mousePos)){
    //        System.out.println(Main.inventory.inventorySlots[i].slotNumber);
    //    }

          System.out.println(Main.inventory.inventorySlots[i].rect.Contains(mousePos));
    }

There may be something wrong with the ".Contains" and not the for loop. Your ".Contains" may only be testing in the first slot. Which would explain why it only works once if the mouse is in the first slot. Also check try printing out the list of slots themselves to make sure you don't have 40 copies of the first slot.

Zanilen
  • 37
  • 10
  • The slots are copies of another class called element, and the element class does not contain a Rectangle class so I created one in order to use a Contains method for the mouse position. Nevertheless, I will give this a shot. – huehuehuehuehue Aug 22 '15 at 03:12
  • Edited to include the Rect class. Using your method proved that Contains was returning false in all instances – huehuehuehuehue Aug 22 '15 at 03:18
  • try printing out pos.x and pos.y and for the y value test try switch and < and > signs – Zanilen Aug 22 '15 at 03:22
  • Hmm my rectangles aren't being created correctly... very strange. – huehuehuehuehue Aug 22 '15 at 03:33
  • I posted it at the bottom. For some reason, even though I am setting the rectangles according to the element, the values are not the same. I'm not sure why this is happening. I set a breakpoint to check the values and only the x values are the same – huehuehuehuehue Aug 22 '15 at 03:43
  • If you are looping through and creating every inventory slot like that make sure you reset Element e as a "new Element()". or when you change it it will change for every inventory slot that has been created. – Zanilen Aug 22 '15 at 03:50
  • createNewElement creates a new Element object every time it is called, so that shouldn't be the issue – huehuehuehuehue Aug 22 '15 at 03:53
  • it sets Element e to equal that new Element. it does not reset Element e, it just assigns it a new value – Zanilen Aug 22 '15 at 03:56
  • Ohhh I get it. I reset the position of the Element to 0, 0 after its stored and now it screwed everything up lol – huehuehuehuehue Aug 22 '15 at 03:59