huehuehuehuehue
huehuehuehuehue

Reputation: 3

Java for loop not running full loop

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);

Upvotes: 0

Views: 989

Answers (1)

Zanilen
Zanilen

Reputation: 37

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.

Upvotes: 1

Related Questions