java Libgdx 鼠标刚刚点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17644429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Libgdx Mouse just clicked
提问by LeSam
I'm trying to get when the mouse just clicked, not when the mouse is pressed. I mean I use a code in a loop and if I detect if the mouse is pressed the code will execute a lot of time, but I want execute the code only Once, when the mouse has just clicked.
我试图在鼠标刚刚点击时获得,而不是在按下鼠标时获得。我的意思是我在循环中使用代码,如果我检测到是否按下鼠标,代码将执行很多时间,但我只想在鼠标刚刚单击时执行一次代码。
This is my code :
这是我的代码:
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
//Some stuff
}
回答by Logan Pickup
See http://code.google.com/p/libgdx/wiki/InputEvent- you need to handle input events instead of polling, by extending InputProcessor and passing your custom input processor to Gdx.input.setInputProcessor().
请参阅http://code.google.com/p/libgdx/wiki/InputEvent- 您需要通过扩展 InputProcessor 并将自定义输入处理器传递给 Gdx.input.setInputProcessor() 来处理输入事件而不是轮询。
EDIT:
编辑:
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;
}
}
And wherever you want to use that:
无论您想在何处使用它:
MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);
If find it easier to use this pattern:
如果发现使用此模式更容易:
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() {
}
}
回答by nEx.Software
You can use Gdx.input.justTouched(), which is true in the first frame where the mouse is clicked. Or, as the other answer states, you can use an InputProcessor (or InputAdapter) and handle the touchDownevent:
您可以使用Gdx.input.justTouched(),这在单击鼠标的第一帧中是正确的。或者,正如其他答案所述,您可以使用 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
}
}
});
回答by MarsPeople
Without InputProcessor you can use easy like this in your render loop:
如果没有 InputProcessor,您可以在渲染循环中使用这样的简单方法:
@Override
public void render(float delta) {
if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)){
//TODO:
}
}

