我需要检查是否使用InputProcessor按下了某个键。我看到网络周围的各种解决方案一旦按下一个键就将布尔值设置为true,并在释放时将其设置为false。这感觉更像是一个黑客,然后是一个很好的解决方案,让我解释一下。
静态方法Gdx.input.isKeyPressed(int key)
和Gdx.input.isKeyJustPressed(int key)
正是他们应该做的事情。但是当我实现InputProcessor
Interface
时,我没有得到这个功能。我只得到:
public boolean keyDown(int keycode) {
return false;
}
public boolean keyUp(int keycode) {
return false;
}
这些只注册一次,基本上是keyDown == Gdx.input.isKeyJustPressed(int key)
。现在,对于每个键,我想添加功能,我必须在这些方法中创建一个布尔值。然后我需要在其他方法中创建另一堆if语句,并在需要时调用它。
所以而不是:
public boolean keyDown(int keycode) {
if (keycode == Keys.Left) //do stuff ;
//etc...
return false;
}
我必须这样做:
boolean leftHold = false;
public boolean keyDown(int keycode) {
if (keycode == Keys.Left) leftHold = true;
//etc...
return false;
}
public boolean keyUp(int keycode) {
if (keycode == Keys.Left) leftHold = false;
//etc...
return false;
}
public void ProcessingTheInputWeGetFromTheInputPorcessor() //starting to feel ridiculous...
{
if (leftHold) //Do stuff ;
//etc..
}
//And call this method each frame on the object:
controlledObject.ProcessingTheInputWeGetFromTheInputPorcessor();
那么检测用InputProcessor保存的密钥的正确解决方案是什么。
答案 0 :(得分:0)
为此不使用InputProcessor,使用InputProcessor立即处理输入,例如
public boolean keyDown(int keyCode) {
if (keyCode == Keys.Escape) {
closeGame();
}
if (keyCode == Keys.Left) {
startMovingLeft();
}
}
如果你想连续检查循环,你只需使用静态方法Gdx.input.isKeyPressed(int key)
,例如
public void loop() {
if (Gdx.input.isKeyPressed(Keys.Left)) {
moveLeft();
}
}
但是不建议这样做,因为如果你在循环之间按键,你可能会错过按键,当然不建议在渲染循环中使用,因为你按下按键时的速率取决于帧速率。
感谢没有人帮我改进答案。