2015-12-09 27 views
5

Per il mio gioco ho implementato un sistema di inventario. Quando si fa clic sullo schermo, un MousePressedEvent viene passato attraverso tutto il layers nel gioco, a tutti gli oggetti che ereditano EventListener (My EventListener). La classe EventListener funziona bene e, utilizzandola come mostrato di seguito, sono riuscito a ottenere il mio inventario in modo da poter rimuovere gli elementi da uno slot e reinserirli. Quello che mi piacerebbe fare comunque è essere in grado di toglierli da qualsiasi slot che contenga oggetti e metterli in qualsiasi altro slot (purché lo spazio di destinazione sia vuoto). Ho pensato che ciò che avrei consentito sarebbe stato questo, poiché nella dichiarazione if non controllo che se lo slot è selezionato, lo aggiungo allo slot a prescindere. Ma questo in realtà non funziona. Qualche idea?Java: consentire l'azione di rilascio nel mio inventario?

Codice in Slot.java classe:

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

In pseudo-codice:

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 

Grazie :)

+1

Non vedo come questo codice si riferisce alla descrizione. Riesci a mettere a nudo il codice per un esempio minimo e dare una spiegazione di cosa sta facendo e cosa non sta facendo in termini di programmazione come liste e loop e se e così via? I concetti del tuo gioco non sono veramente rilevanti per il codice. – zapl

+1

@zapl meglio? Ho aggiunto una versione di pseudo codice per chiarire le cose –

+1

Puoi specificare quale parte del tuo pseudo codice non funziona come ti aspetti? Ci sono molti casi limite ... –

risposta

2

Aggiungendo a righe di stampa in ogni else if dichiarazione, ho scoperto che quando provo per aggiungere un elemento dallo slot temporaneo in un altro slot, lo slot temporaneo è nullo. Ciò è dovuto al fatto che quando viene creato lo slot temporaneo, viene creato dall'istanza dello slot selezionato per la prima volta, quindi quello che si sta tentando di aggiungere non ha accesso allo slot temporaneo. Per aggirare questo problema, ho spostato lo slot temporaneo come variabile per istanza, rendendolo statico. Il codice ora funziona bene