Java: 允许在我的物品栏中进行掉落操作?

Java: Allowing drop action in my inventory?

对于我的游戏,我实施了库存系统。单击屏幕时,一个 MousePressedEvent 将通过游戏中的所有 layers 传递给所有继承 EventListener 的对象(我的 EventListener)。 EventListener class 工作正常,并且如下所示使用它,我已经设法获得我的库存,以便您可以从插槽中移除项目并将它们放回原处。然而,我想做的是能够将它们从任何包含物品的插槽中取出,并将它们放置在任何其他插槽中(只要目标插槽为空)。我认为我已经允许这样做,因为在 if 语句中我不检查是否选择了插槽,无论如何我都将它添加到插槽中。但这实际上行不通。有什么想法吗?

Slot.java中的代码class:

public boolean onMousePressed(MousePressedEvent e) {
    Point p = new Point(Mouse.getX(), Mouse.getY());
    if (!this.getBounds().contains(p)) return false;
    boolean left = (e.getButton() == MouseEvent.BUTTON1);
    boolean right = (e.getButton() == MouseEvent.BUTTON3);
    boolean hasItems = (items.size() > 0);
    if (this.getBounds().contains(p)){
        if (right && !selected && hasItems){
            select(true);
            s = new Slot(new Vector2i(Mouse.getX(), Mouse.getY()));
            addComponent(s);
            s.add(items.get(0));
            remove(items.get(items.size() - 1));
        } else if (right && selected){
            s.add(items.get(0));
            remove(items.get(items.size() - 1));
            if (items.size() == 0) {
                setBackgroundImage(ImageUtil.getImage("/ui/panels/inventory/slot.png"));
                selected = false;
                return true;
            }
            return true;
        } else if ((left || right) && s==null) {
            return true;
        } else if (left && s != null){ //If left clicked, add to the slot from s regardless of if we are selected.
            add(s.getItems().get(0));
            s.remove(s.getItems().get(s.getItems().size() - 1));
            if (s.getItems().size() == 0){
                s.setBackgroundImage(ImageUtil.getImage("/ui/panels/inventory/slot.png"));
                removeComponent(s);
                s = null;
                selected = false;
                return true;
            }
        }
    }
    return false;
}

在伪代码中:

If (Mouse is clicked) :
  if (the mouse isn't the bounds of the slot) return false (alert we haven't handled the event)
  if (we contain the mouse cursor) :
    if (right is pressed and we aren't selected) :
      select
      create a temporary slot at the mouse location
      remove item from this slot
      add it to the temporary slot
      return true
    else if (right is pressed and we are selected) :
      add item to temporary slot
      remove item from selected slot
      return true
    else if (we press left or right while temporary slot is null) :
      return true (tell the dispatcher we have handled the event)
    //This following else if statement is supposed to add an item to a clicked slot whether that slot is selected or not, but doesn't work
    else if (left is pressed and temporary slot isn't null) :
      add the item to the clicked slot
      remove it from the temporary one
      return true
  return false if none of the above applies

谢谢:)

通过在每个 else if 语句中添加打印行,我发现当我尝试将一个项目从临时槽添加到另一个槽时,临时槽为空。这是因为创建临时插槽时,它是由您第一次选择的插槽实例创建的,因此您尝试添加的那个没有访问临时插槽的权限。为了解决这个问题,我将临时槽作为每个实例变量移动,方法是将其设为静态。代码现在可以正常工作了