2015-12-09 9 views
5

मेरे खेल के लिए मैंने एक इन्वेंट्री सिस्टम लागू किया है। जब स्क्रीन क्लिक किया जाता है, एक MousePressedEvent सभी वस्तुओं है कि वारिस EventListener (मेरे EventListener) के लिए खेल में सभी layers के माध्यम से पारित कर दिया है,। 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 

+1

मैं कैसे इस कोड को वर्णन करने के लिए संबंधित नहीं दिख रहा। क्या आप कोड को कम से कम उदाहरण में फिसल सकते हैं और यह क्या कर रहे हैं और यह प्रोग्रामिंग शर्तों जैसे सूचियों और लूप और आईईएस और ऐसा नहीं कर रहा है, इसका स्पष्टीकरण दे सकता है? आपके गेम की अवधारणा कोड के लिए वास्तव में प्रासंगिक नहीं हैं। – zapl

+1

@zapl बेहतर है? मैंने चीजों को स्पष्ट करने के लिए एक छद्म कोड संस्करण जोड़ा –

+1

क्या आप निर्दिष्ट कर सकते हैं कि आपके छद्म कोड का कौन सा हिस्सा काम नहीं कर रहा है? वहाँ किनारे मामलों का एक बहुत मैं वास्तव में इसका उल्टा मतलब, जैसे "मैं एक सूची से एक वस्तु को हटाने और पाश की अगले चरण में एक अशक्त है" :) सबसे महत्वपूर्ण बात, मैं अभी भी डॉन है .... –

उत्तर

2

:) धन्यवाद प्रत्येक else if बयान में प्रिंट लाइनों में जोड़ कर, मैंने पाया कि जब मैं कोशिश अस्थायी स्लॉट से किसी अन्य स्लॉट में कोई आइटम जोड़ने के लिए, अस्थायी स्लॉट शून्य है। यह इस तथ्य के कारण है कि जब अस्थायी स्लॉट बनाया जाता है, तो यह पहली बार चुने गए स्लॉट के उदाहरण द्वारा बनाया जाता है, इसलिए जिसे आप जोड़ना चाहते हैं, उसमें अस्थायी स्लॉट तक पहुंच नहीं है। इसके आस-पास पहुंचने के लिए, मैंने स्थैतिक बनाकर अस्थायी चर के रूप में अस्थायी स्लॉट को स्थानांतरित कर दिया। कोड अब ठीक काम करता है

संबंधित मुद्दे