我试图获取鼠标刚刚单击的时间,而不是按下鼠标的时间。 我的意思是我在循环中使用代码,如果我检测到鼠标是否被按下,代码将执行很多时间,但我只想在鼠标刚刚单击时执行一次代码。
这是我的代码:
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
//Some stuff
}
最佳答案
参见 http://code.google.com/p/libgdx/wiki/InputEvent - 您需要通过扩展 InputProcessor 并将自定义输入处理器传递给 Gdx.input.setInputProcessor() 来处理输入事件而不是轮询。
编辑:
public class MyInputProcessor implements InputProcessor {
@Override
public boolean touchDown (int x, int y, int pointer, int button) {
if (button == Input.Buttons.LEFT) {
// Some stuff
return true;
}
return false;
}
}
无论你想在哪里使用它:
MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);
如果发现使用这种模式更容易:
class AwesomeGameClass {
public void init() {
Gdx.input.setInputProcessor(new InputProcessor() {
@Override
public boolean TouchDown(int x, int y, int pointer, int button) {
if (button == Input.Buttons.LEFT) {
onMouseDown();
return true;
}
return false
}
... the other implementations for InputProcessor go here, if you're using Eclipse or Intellij they'll add them in automatically ...
});
}
private void onMouseDown() {
}
}
关于java - Libgdx 鼠标刚刚点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17644429/