Libgdx Mouse just clicked

java, libgdx

Solution

See http://code.google.com/p/libgdx/wiki/InputEvent - you need to handle input events instead of polling, by extending InputProcessor and passing your custom input processor to Gdx.input.setInputProcessor().

EDIT:

public class MyInputProcessor implements InputProcessor {
   @Override
   public boolean touchDown (int x, int y, int pointer, int button) {
      if (button == Input.Buttons.LEFT) {
          // Some stuff
          return true;     
      }
      return false;
   }
}

And wherever you want to use that:

MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);

If find it easier to use this pattern:

class AwesomeGameClass {
    public void init() {
        Gdx.input.setInputProcessor(new InputProcessor() {
            @Override
            public boolean touchDown(int x, int y, int pointer, int button) {
                if (button == Input.Buttons.LEFT) {
                    onMouseDown();
                    return true;
                }
                return false
            }
            
            ... the other implementations for InputProcessor go here, if you're using Eclipse or Intellij they'll add them in automatically ...
        });
    }
    
    private void onMouseDown() {
    }
}

Problem

I'm trying to get when the mouse just clicked, not when the mouse is pressed. I mean I use a code in a loop and if I detect if the mouse is pressed the code will execute a lot of time, but I want execute the code only Once, when the mouse has just clicked. This is my code : ``` if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)){ //Some stuff } ```

Original source