Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when an actor is touched in libgdx?

I am using "Gdx.input.isTouched()" in the render method of my Screen method, to know where is touched, but when the touch is dragged in the screen, it also activates the events i want only when an actor is touched.

Is there any listener to know when an Actor is touched, but the event is not the dragged one, im doing it with sprites.

like image 292
Rudy_TM Avatar asked Dec 22 '22 02:12

Rudy_TM


2 Answers

See this wiki page about scene2d in LibGDX. Specifically the part about Input handling.

Basically you have to override one or more of these methods in your Actor:

public boolean touchDown (float x, float y, int pointer) {
    return false;
}

public void touchUp (float x, float y, int pointer) {
}

public void touchDragged (float x, float y, int pointer) {
}

public boolean touchMoved (float x, float y) {
    return false;
}

public boolean scrolled (int amount) {
    return false;
}

public boolean keyDown (int keycode) {
    return false;
}

public boolean keyUp (int keycode) {
    return false;
}

public boolean keyTyped (char character) {
    return false;
}
like image 149
Ludevik Avatar answered Dec 27 '22 01:12

Ludevik


in libGDX Actor have a listener inside. Example if you want check when a button is press, or is check, you call : button.isPressed(), button.isCheck(), it return boolean.

like image 25
ManhPhi Avatar answered Dec 27 '22 02:12

ManhPhi