好的,我已经为我的A级计算项目编写了游戏,并且我已经开始实施库存系统。但是,我的鼠标输入检测是丢掉鼠标点击事件4次,而不仅仅是一次,我不明白为什么。它导致问题,因为当我点击一个库存项目时,它试图从插槽列表中删除4个项目而不仅仅是一个项目,当占用少于4个项目时,它会抛出IndexOutOfBoundsException
槽。
这是鼠标事件类:
package com.ritcat14.GotYourSix.input;
import com.ritcat14.GotYourSix.events.EventListener;
import com.ritcat14.GotYourSix.events.types.MouseMovedEvent;
import com.ritcat14.GotYourSix.events.types.MousePressedEvent;
import com.ritcat14.GotYourSix.events.types.MouseReleasedEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Mouse implements MouseListener, MouseMotionListener{
private static int mouseX = -1, mouseY = -1, mouseB = -1;
public static boolean left = false, right = false;
public EventListener eventListener = null;
public Mouse(EventListener listener) {
this.eventListener = listener;
}
public static int getX(){
return mouseX;
}
public static int getY(){
return mouseY;
}
public static int getButton(){
return mouseB;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
mouseB = e.getButton();
MousePressedEvent event = new MousePressedEvent(e.getButton(), e.getX(), e.getY());
eventListener.onEvent(event);
}
public void mouseReleased(MouseEvent e) {
mouseB = MouseEvent.NOBUTTON;
MouseReleasedEvent event = new MouseReleasedEvent(e.getButton(), e.getX(), e.getY());
eventListener.onEvent(event);
}
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
MouseMovedEvent event = new MouseMovedEvent(e.getX(), e.getY(), true);
eventListener.onEvent(event);
}
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
MouseMovedEvent event = new MouseMovedEvent(e.getX(), e.getY(), false);
eventListener.onEvent(event);
}
}
以下是Slot.java
班级中我试图使用它的地点和方式。更新方法:
public void update(){
super.update();
Rectangle rect = new Rectangle(getAbsolutePosition().x - 50, getAbsolutePosition().y - 50, 50, 50);
Point p = new Point(Mouse.getX(), Mouse.getY());
if (Mouse.getButton() == MouseEvent.BUTTON1 && rect.contains(p) && !selected && items.size() > 0) {
selected = true;
s = new Slot(new Vector2i(Mouse.getX() - 25, Mouse.getY() - 25));
addComponent(s);
s.add(items.get(items.size() - 1));
items.remove(items.get(items.size() - 1));
} else if (Mouse.getButton() == MouseEvent.BUTTON1 && rect.contains(p) && selected) {
clicked ++;
s.add(items.get(items.size() - 1));
items.remove(items.get(items.size() - 1));
} else if (Mouse.getButton() == MouseEvent.BUTTON3 && rect.contains(p) && selected) {
items.add(s.getItems().get(s.getItems().size() - 1));
s.getItems().remove(s.getItems().size() - 1);
}
}