Java: allow drop action in my inventory?

In my game, I implemented an inventory system. When you click on the screen, MousePressedEvent goes through all layers in the game, to all objects that inherit EventListener (My EventListener ). The EventListener class EventListener fine, and using it as shown below, I managed to get my inventory so that you can remove items from the slot and return them back. However, I would like to extract them from any slot containing the elements and place them in any other slot (if the target slot is empty). I thought that I would allow this, since in the if I did not check that if a slot is selected, I add it to the slot independently. But it doesn’t actually work. Any ideas?

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

In pseudo code:

 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 

Thanks:)

+5
source share
1 answer

By adding print lines to each else if , I find that when I try to add an item from a time slot to another slot, the time interval is zero. This is due to the fact that when creating a time slot, it is created by an instance of the slot you selected, so the one you are trying to add does not have access to the time interval. To get around this, I moved the time slot as a variable for each instance, making it static. Code now works great

+2
source

Source: https://habr.com/ru/post/1237777/


All Articles