Problem with repeating while MousePicking

Hi all! I have encountered a Problem with my way of handling MouseInputs and still not found a way to solve it:



In my game I click on an Objects and then click on the Terrain to mark a target point (where the object should move to). Therefore in the update() function I use



If(MouseInput.get().isButtonDown(0))

{

       // all MousePickingCode and Ray Code

}



The problem is that when I press the MouseButton the Ray always delivers about more than 5 Points on the Terrain (I guess this is because the Ray is updated every Frame and as long as I hold down the Button Rays are spawned. If I want to spawn however some Particles as a visual hint on the Target Point I always get 5+ Particle Spawns, instead of a preferred single one.

I investigated Age of Empires 1 therefore and found out that a unit is only marked when the Mouse is being released and not while the MouseButton is still pressed. Now I want to ask if there's a way I can achieve this "only when Mousebutton is released" with the MouseInput or is my approach to handle this Actions wrong anyways and there is another solution? :?



Edit: now I found something like MouseInputListener. My Assumption is, that when I set the boolean pressed to false (like written in the API) it acts as "released" only:


MouseInput.get().addListener(new MouseInputListener()
                {
                   public void onButton(int button, boolean pressed, int x, int y)
                   {
                      pressed = false;
                   };
                   public void onWheel(int wheelDelta, int x, int y){};
                   public void onMove(int xDelta, int yDelta, int newX, int newY){};
                });
               
                MouseInput.get().update();

and later on I use again in the update():

If(MouseInput.get().isButtonDown(0))
{
       // all MousePickingCode and Ray Code...
       // ... + output
       System.out.println(pickedObject);
}



But again, when I press and hold down the MouseButton I get a repeated output before the button is released  :(

You could use a boolean and test if the MouseInput.get().isButtonDown(0) is changed




       if(!MouseInput.get().isButtonDown(0)) {
          if(mouseDown) {
             // mouse was just released
              System.out.println("release");
             
          }
          mouseDown = false;
       }
       else {
          if(!mouseDown) {
             // mouse just pressed
              System.out.println("press");
          }
          mouseDown = true;
       }



Or your way


MouseInput.get().addListener(new MouseInputListener()
                {
                   public void onButton(int button, boolean pressed, int x, int y)
                   {
                      if(pressed) pickRequested = true;
                   };
                   public void onWheel(int wheelDelta, int x, int y){};
                   public void onMove(int xDelta, int yDelta, int newX, int newY){};
                });
               
                MouseInput.get().update();




if(pickRequested)
{
       // all MousePickingCode and Ray Code...
       // ... + output
       System.out.println(pickedObject);
       pickRequested = false;
}



But you really should be using GameControlManager or something

Wow, this helped me a lot and works perfectly!

Thx!!! :slight_smile: :slight_smile: :slight_smile: