我不确定这是否与我的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);
这是不是很清楚你问什么 - 对我来说,看起来如果这些库存插槽不重叠,那么你应该只希望看到一个打印。 – argentage
我在问是否有什么突出的东西会给我点击时只读取元素[0]的问题?如果我点击任何其他inventorySlot,控制台中不显示任何内容。 – huehuehuehuehue
我应该用什么来代替?我有Vector2鼠标位置和一个矩形数组来检查哪个矩形包含鼠标。 – huehuehuehuehue