I follow this [a little old tutorial for 2d java games here] [1].
I have a basic applet that runs in a stream using mouselistener.
On the left mouse button I can remove up to 10 blocks (balls) from the bottom of the window. On the right button, click "shoot", and if I hit a pigeon, it will be removed from the screen.
I noticed that sometimes right clicks do not get. This is not necessary when a lot of things happen on the screen, although never in the beginning, until everything starts. In the worst case, it may take 3 or 4 clicks to register.
I assume that I am doing something clearly wrong in my code, but I'm not sure what. My first thought was that for loops that go through each object every frame, in order to recount their position or to check if they were โshotโ? Could they "block" the mousetrap ?!
Any advice on how to debug this is welcome!
********* EDIT ***********
Good. I took very good advice below and reduced the code to the smallest fragment that reproduces the error. I think I had in my head what all of the loops and complexity are causing the problems, so I included so much in my first code.
, , . mouselistener, . , "" .
3 4 , , . , :
:
package javacooperation;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
public class ClayPigeonGame extends Applet implements Runnable, MouseListener {
private boolean flag = true;
private int click_count = 0;
private boolean game_running;
public void init(){
game_running=true;
addMouseListener(this);
}
public void start() {
Thread th = new Thread(this);
th.start();
}
public void stop(){
game_running=false;
}
public void destroy() { }
public void run(){
while(game_running) {
repaint();
try{
Thread.sleep(20);
} catch (InterruptedException ex){ }
}
}
public void paint(Graphics g){ }
public void mouseClicked(MouseEvent e) {
switch (e.getButton()){
case MouseEvent.BUTTON1:
case MouseEvent.BUTTON2:
break;
case MouseEvent.BUTTON3:
click_count ++;
System.out.println("Right click count: " + click_count);
break;
default:
break;
}
}
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}