我在使用libgdx的scene2d时遇到了问题。我找不到任何方法来检查演员是否被触摸。我只能找到一些方法,告诉我演员是否被触摸或释放。在我的游戏中,当演员被按下并保持时,应该每帧执行一些操作,而不仅仅是在我触摸它的那一刻执行一次。当我松开手指时,我想停止这些操作。
我在使用libgdx的scene2d时遇到了问题。我找不到任何方法来检查演员是否被触摸。我只能找到一些方法,告诉我演员是否被触摸或释放。在我的游戏中,当演员被按下并保持时,应该每帧执行一些操作,而不仅仅是在我触摸它的那一刻执行一次。当我松开手指时,我想停止这些操作。
您可以在您的InputListener
中跟踪此信息。创建一个布尔型字段isTouched
,当您得到touchDown
时设置为true,当您得到touchUp
时设置为false。我在我的俯视射击游戏中使用了这种方法,它效果非常好。
gdx.app.log("","touched"+touchdown);
首先设置输入处理器...
Gdx.input.setInputProcessor(mystage);
optone.addListener(new InputListener() {
@Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
boolean touchdown=true;
//do your stuff
//it will work when finger is released..
}
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
boolean touchdown=false;
//do your stuff it will work when u touched your actor
return true;
}
});
我曾经遇到过类似的问题,而且我还需要知道当前坐标;所以我解决了这个问题,就像这样:
首先,我们扩展了标准监听器:
class MyClickListener extends ClickListener {
float x, y = 0;
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
this.x = x;
this.y = y;
return super.touchDown(event, x, y, pointer, button);
}
@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
this.x = x;
this.y = y;
super.touchDragged(event, x, y, pointer);
}
}
然后将一个实例添加到Actor中:
class MyActor extends Actor {
private final MyClickListener listener = new MyClickListener();
MyActor() {
addListener(listener);
}
...
}
if (listener.getPressedButton() >= 0)
System.out.println(listener.x + "; " + listener.y);