3
\$\begingroup\$

I develop a shooting game. To make it harder I want to make the reticle shaky/move randomly.

I use Robot class to move random as suggest here.

But I modified it by adding an infinity loop and by surrounding it in a new Thread.

The shaky mouse cursor is running well, but I can't shoot the target because the infinity loop holds mouseClicked(MouseEvent e) to make a response. I'm confused because I've already tried to make a special thread for the random displacement of the mouse cursor but it still makes no response for my mouseClicked(MouseEvent e).

Any idea/solution please?

UPDATED POST : i include the code here :

In GPanel.java :

1. For handle input from mouse hardware (from user when clicked the mouse)

@Override
public void mouseClicked(MouseEvent e) {
     int x = e.getX();
     int y = e.getY();
     System.out.println(ter.getInnerX()+"---------------"+ter.getInnerY());
    System.out.println(x+"=========================="+y);

   if(x>=ter.getInnerX()&&x<=ter.getInnerX()+54)
   {
       System.out.println("masuk");
       if(y>=ter.getInnerY()&&(y<=ter.getInnerY()+95))
       {
           System.out.println("kena");
       }
   }
}

2. Robot class Implemented :

@Override public void run(){

while (true){     
    try {
        updateCar();
        checkCollision();
        keylistener();
        Thread.sleep(20);  
Robot robot = new Robot();
Random random = new Random();
boolean masuk=true;
int SECONDS=40;
while (true) {
    if(masuk)
    {
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x+1, MouseInfo.getPointerInfo().getLocation().y);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x+1, MouseInfo.getPointerInfo().getLocation().y+1);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y+1);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x-1, MouseInfo.getPointerInfo().getLocation().y+1);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x-1, MouseInfo.getPointerInfo().getLocation().y);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y-1);
        masuk=false;
    }
else
    {
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x-1, MouseInfo.getPointerInfo().getLocation().y);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x-1, MouseInfo.getPointerInfo().getLocation().y-1);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y-1);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x+1, MouseInfo.getPointerInfo().getLocation().y-1);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x+1, MouseInfo.getPointerInfo().getLocation().y);
        Thread.sleep(SECONDS);
        robot.mouseMove(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y+1);
        masuk=true;
    }

}
    } catch (Exception e) {
        e.printStackTrace();
    }
    repaint();
}}

Number 1 for detect user's input by click the mouse.

Number 2 for implemented Robot.class to make shaky mouse cursor.

Both of number 1 and 2 are in GPanel.java

\$\endgroup\$
2
  • \$\begingroup\$ Can you include relevant code part - creating the thread and handling events? \$\endgroup\$
    – wondra
    Nov 12, 2015 at 17:51
  • \$\begingroup\$ @wondra Thanks for reply Wondra. I already update the code for tracing. \$\endgroup\$
    – Mr. Mike
    Nov 13, 2015 at 1:49

1 Answer 1

2
\$\begingroup\$

Try Change mouseClicked into mousePressed. Seems work for me...

Here is the complete code :

@Override
public void mousePressed(MouseEvent e) {
     int x = e.getX();
     int y = e.getY();
     System.out.println(ter.getInnerX()+"---------------"+ter.getInnerY());
    System.out.println(x+"=========================="+y);

   if(x>=ter.getInnerX()&&x<=ter.getInnerX()+54)
   {
       System.out.println("masuk");
       if(y>=ter.getInnerY()&&(y<=ter.getInnerY()+95))
       {
           System.out.println("kena");
       }
   }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .