2015-12-09 27 views
5

對於我的遊戲,我實施了一個庫存系統。點擊屏幕時,MousePressedEvent通過遊戲中的所有layers傳遞給繼承EventListener(我的EventListener)的所有對象。 EventListener類工作正常,並使用它如下所示,我設法得到我的庫存,以便您可以從一個插槽中刪除項目,並把它們放回去。然而,我想要做的是能夠將它們從包含項目的任何插槽中取出,並將它們放置在任何其他插槽中(只要目標插槽爲空)。我想我有什麼可以允許這一點,如在if聲明我不檢查,如果插槽被選中,我將它添加到插槽無論如何。但這並不實際。有任何想法嗎?Java:在我的庫存中允許刪除操作?

守則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 

謝謝:)

+1

我不明白這段代碼是如何與描述相關的。你能否將代碼剝離爲一個最簡單的例子,並解釋它在做什麼以及它在編程方面沒有做什麼,如列表和循環,ifs等等?您的遊戲概念與代碼無關。 – zapl

+1

@zapl更好嗎?我添加了一個僞代碼版本來澄清事情 –

+1

你能指定你的僞代碼的哪一部分沒有按照你的預期工作嗎?有很多邊緣情況.... –

回答

2

通過在每個else if聲明中加入在打印行,我發現,當我嘗試要將臨時插槽中的項目添加到另一個插槽中,臨時插槽爲空。這是由於這樣一個事實,即創建臨時插槽時,它是由您第一次選擇的插槽實例創建的,因此您嘗試添加的插槽無法訪問臨時插槽。爲了解決這個問題,我將臨時插槽作爲每個實例變量移動,並將其設置爲靜態。代碼現在工作正常