2015-08-22 73 views
0

我不确定这是否与我的for循环或矩形位置有关,但我已设置断点并使用调试绘制来绘制矩形,并且它们显示为正确。Java for loop未运行完整循环

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

发生什么事情是写入控制台的唯一矩形是第一个。我想要发生的是每次右键单击为true并且布尔值为true时,循环遍历矩形列表并找出哪个包含mousePos。当矩形被点击时,控制台中只出现“0”,所以我知道我没有得到任何其他库存槽号重叠。

另一件可能发生的事情是,循环可能没有完全运行,这将是我的点击方法中的问题。

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

这是不是很清楚你问什么 - 对我来说,看起来如果这些库存插槽不重叠,那么你应该只希望看到一个打印。 – argentage

+0

我在问是否有什么突出的东西会给我点击时只读取元素[0]的问题?如果我点击任何其他inventorySlot,控制台中不显示任何内容。 – huehuehuehuehue

+0

我应该用什么来代替?我有Vector2鼠标位置和一个矩形数组来检查哪个矩形包含鼠标。 – huehuehuehuehue

回答

1

for循环一直循环。尝试在每个循环中打印出if语句的状态。

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

“.Contains”可能有问题,而不是for循环。 您的“.Contains”可能只能在第一个插槽中测试。这可以解释为什么只有当鼠标位于第一个插槽时才能工作一次。另外检查尝试打印出自己的插槽列表,以确保您没有40个第一个插槽的副本。

+0

插槽是另一个称为元素的类的副本,并且元素类不包含Rectangle类,所以我创建了一个以便为鼠标位置使用Contains方法。不过,我会给这个镜头。 – huehuehuehuehue

+0

编辑为包含Rect类。使用你的方法证明Contains在所有情况下都返回false – huehuehuehuehue

+0

尝试打印pos.x和pos.y,并为y值test test开关和< and >标志 – Zanilen