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

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语句中添加打印行,我发现当我尝试将项目从临时插槽添加到另一个插槽时,该临时插槽为null。这是由于以下事实:创建临时插槽时,它是由您第一次选择的插槽实例创建的,因此您要添加的临时插槽无法访问临时插槽。为了解决这个问题,我通过将temp槽设置为静态将其作为每个实例的变量移动。该代码现在可以正常工作

09-26 21:47
查看更多