Buttons with android and LibGDX

1

Hi, I created a little button on android, with libGDX very simple, but the problem is this:

I have assigned a function that writes on screen ("hola") something like this:

if(boton.sePulsa()){
   System.out.println("hola")
  }

and of course I print on the screen 3 times or more, "hola" , and I want that until you do not press again do not print anything. Any help pls.

// I edit the function sePulsa() is the following:

public boolean sePulsaElBoton() {
    return Gdx.input.isTouched() && Gdx.input.getX() >= xMinima && Gdx.input.getX() <= xMaxima &&
            Gdx.input.getY() >= yMinima && Gdx.input.getY() <= yMaxima;
}

xMaxima,Xminima , Yminima ,YMaxima are of type float .

    
asked by Ramosaurio 16.08.2016 в 18:29
source

3 answers

2

The most common option is to implement InputProcessor and using the method TouchDown () you could detect when the screen or your case, a button is pressed.

link

public class MyInputProcessor implements InputProcessor {

...

   @Override
   public boolean touchDown (int x, int y, int pointer, int button) {
      return false;
   }

...

}

In the case of your implementation, you could simply use:

public boolean sePulsaElBoton() {
    return Gdx.input.isTouched();
}

or JustTouched :

public boolean sePulsaElBoton() {
    return Gdx.input.justTouched();
}
    
answered by 16.08.2016 / 22:04
source
1

Use Gdx.input.justTouched();

Gdx.input.isTouched() notify if you have touched or are touching the screen, so if you take 2 seconds by pressing the button, it will print on the screen (in your case) as many messages as you can in that time.

    
answered by 01.03.2017 в 09:57
0

This question is old but, if the button is a Actor as are the classes TextButton , Button , Table , Image and others, you can add a EventListener and ready

button.addListener(new ClickListener(){
    @Override
    public void clicked(InputEvent event, float x, float y) {
        super.clicked(event, x, y);
        System.out.println("Este botón se pulso!");
    }
});

You have much more control, no need to program it yourself.

You can "Override" many methods ...

The most that I personally use are:

  • InputListener : I use it when I want to detect when it is being pressed and when it is stopped, but it has much more methods for "Override".

  • ClickListener : I use it when I want to detect only one pulsaso, same has much more methods for "Override"

  • ActorGestureListener : I use it when I want to detect a "Long press" which is when someone presses and presses a button for more than 1 ~ second

Here is a list of all EventListeners :

  • InputListener

  • ClickListener

  • ActorGestureListener

  • ChangeListener

  • DragListener

  • DragScrollListener

  • FocusListener

  • TextArea.TextAreaListener

  • TextField.TextFieldClickListener

  • TextTooltip

  • Tooltip

answered by 10.04.2017 в 11:30