Hello, I have created a small button in android, with very simple libGDX, but the problem is the following:
I have assigned a function that writes ("hola")
something like this to the screen:
if(boton.sePulsa()){
System.out.println("hola")
}
and of course, it prints on the screen 3 times or more, "hola"
and I want it to not print anything until it is pressed again. Any help pls.
//I edit the function sePulsa()
is as follows:
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
.
The most common option is to implement InputProcessor and through the TouchDown() method you could detect when the screen or your case, a button is pressed.
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/InputProcessor.html
In the case of your implementation you could simply use:
or JustTouched :
Uses
Gdx.input.justTouched();
Gdx.input.isTouched()
notifies if you have touched or are touching the screen, so if you spend 2 seconds pressing the button, it will print on the screen (in your case) as many messages as it can in that time.This question is old but, if the button is a
Actor
like the classesTextButton
,Button
,Table
,Image
and others, you can add a to itEventListener
and that's itYou have much more control, without the need to program it yourself.
You can "Override" many methods...
The ones I personally use are:
InputListener
: I use it when I want to detect when it is being pressed and when it is released, but it has much more methods for "Override".ClickListener
: I use it when I want to detect only one pulse, it still has many more methods for "Override"ActorGestureListener
: I use it when I want to detect a "Long press" which is when someone presses and leaves a button pressed for more than 1~ secondHere is a list of all
EventListeners
:InputListener
ClickListener
ActorGestureListener
ChangeListener
DragListener
DragScrollListener
FocusListener
TextArea.TextAreaListener
TextField.TextFieldClickListener
TextTooltip
tool tip