I am doing a game for the university and I would like to ask for help as I can’t create a good mouse controller.
I would like to ask for that code as mine is working properly. Here’s my code:
private void setMouseListeners() {
addMouseListener(new MouseAdapter() {
boolean mousePressed = false;
Thread t = null;
public void mousePressed(MouseEvent me) {
if ( t == null ){
t = new Thread(new Runnable() {
@Override
public void run() {
mousePressed = true;
while (mousePressed) {
try {
Thread.sleep(TIME_PER_SHOT);
if (mousePressed) {shootDefender();}
} catch (InterruptedException e) {}
}
}
});
t.start();
}
}
public void mouseClicked (MouseEvent me){
if ( !mousePressed )
shootDefender();
}
public void mouseReleased(MouseEvent me){
mousePressed = false;
t = null;
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent me) {
mousePosition = me.getPoint();
}
public void mouseMoved(MouseEvent me) {
mousePosition = me.getPoint();
}
});
}
My intention is this to work like in this Geometry Wars Remake from this Basic Game tutorial. Unfortunately, there is no mouse related code.
If someone could fix it I would really appreciate it. Actually, it works, but if you click quickly twice and keep pressing it shootDefender() runs twice and isn’t waiting the TIME_PER_SHOOT value.