How to detect when an actor is touched in libgdx?

情到浓时终转凉″ 提交于 2019-11-28 09:06:27

问题


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.


回答1:


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;
}



回答2:


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.




回答3:


I think you have to implement one of the following method to detect only single time touch not the move event perform by the user.

Gdx.input.justTouched();



回答4:


I think you control in Stage when you have multi-actors.



来源:https://stackoverflow.com/questions/9236175/how-to-detect-when-an-actor-is-touched-in-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!