Libgdx mouse just clicked

I try to get when the mouse clicked, not when the mouse is clicked. I mean, I use the code in a loop, and if I find that the mouse is pressed, the code will execute a lot of time, but I want to execute the code only once, when the mouse just clicked.

This is my code:

if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)){ //Some stuff } 
+6
source share
2 answers

See http://code.google.com/p/libgdx/wiki/InputEvent - you need to handle input events instead of polling, extending the 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 this:

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

If you find it easier to use this template:

 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() { } } 
+9
source

You can use Gdx.input.justTouched() , which is true in the first frame that the mouse is clicked on. Or, as stated in another answer, you can use InputProcessor (or InputAdapter) and handle the touchDown event:

 Gdx.input.setInputProcessor(new InputAdapter() { public boolean touchDown(int screenX, int screenY, int pointer, int button) { if (button == Buttons.LEFT) { // do something } } }); 
+11
source

Source: https://habr.com/ru/post/949426/


All Articles