我试图在鼠标刚刚点击时获取,而不是在按下鼠标时.我的意思是我在循环中使用代码,如果我检测到鼠标是否被按下,代码将执行很多时间,但我只想执行代码一次,当鼠标刚刚单击时.
这是我的代码:
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
//Some stuff
}
Run Code Online (Sandbox Code Playgroud)
Log*_*kup 11
请参阅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;
}
}
Run Code Online (Sandbox Code Playgroud)
无论你想用哪个:
MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);
Run Code Online (Sandbox Code Playgroud)
如果发现使用此模式更容易:
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() {
}
}
Run Code Online (Sandbox Code Playgroud)
nEx*_*are 11
您可以使用Gdx.input.justTouched()
,在单击鼠标的第一帧中为true.或者,正如另一个答案所述,您可以使用InputProcessor(或InputAdapter)并处理touchDown
事件:
Gdx.input.setInputProcessor(new InputAdapter() {
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if (button == Buttons.LEFT) {
// do something
}
}
});
Run Code Online (Sandbox Code Playgroud)