2015-06-20 61 views
0

我正在尝试使用Gdx.input.isKeyPressed();来处理键盘输入。起初我处理了asdw输入,一切都很顺利。但是,当我尝试处理按键输入时,下面几行不起作用。Libgdx认为键盘总是被按下

当我调试它时,我可以看到它始终将ifs视为true(换句话说,他的行为就像所有箭头总是被按下),但是这不会在“asdw”检查中发生。

当我试图将密钥更改为常规字母时,问题仍然存在。

代码:

public void update(float deltaTime) 

    handleDebugInp(deltaTime); 
    updateTestSprites(deltaTime); 
    cameraH.update(deltaTime); 
} 

private void handleDebugInp(float dt) {//for enabled long pressed buttons 
    if(Gdx.app.getType() != ApplicationType.Desktop)//proceed with debugging only on pc 
     return; 

    /*keys movement*/ 
    float spriteSpeed = 5 * dt;//moves 5 meters per second 

    if(Gdx.input.isKeyPressed(Keys.A))//if A is pressed 
     moveCurrSprite(-spriteSpeed, 0);//move left 
    if(Gdx.input.isKeyPressed(Keys.D))//if D is pressed 
     moveCurrSprite(spriteSpeed, 0);//right 
    if(Gdx.input.isKeyPressed(Keys.W))//if W is pressed 
     moveCurrSprite(0, spriteSpeed);//up 
    if(Gdx.input.isKeyPressed(Keys.S))//if S is pressed 
     moveCurrSprite(0, -spriteSpeed);//down 

    /*Camera movment*/ 
    //speed 
    float cameraSpeed = 5 * dt; 
    float accelerationFactor = 5; 

    //sprint 
    if(Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)); 
     cameraSpeed *= accelerationFactor; 

    //movement 
    if(Gdx.input.isKeyPressed(Keys.LEFT)); 
     moveCamera(-cameraSpeed, 0); 
    if(Gdx.input.isKeyPressed(Keys.RIGHT)); 
     moveCamera(cameraSpeed, 0); 
    if(Gdx.input.isKeyPressed(Keys.UP)); 
     moveCamera(0, -cameraSpeed); 
    if(Gdx.input.isKeyPressed(Keys.DOWN)); 
     moveCamera(0, cameraSpeed); 

} 
private void moveCamera(float x, float y) { 
    x += cameraH.getPosition().x; 
    y += cameraH.getPosition().y; 

    cameraH.setPosition(x, y); 
} 

如果有人可能知道是什么问题,我会很高兴,如果他会告诉我。

谢谢。

回答

2

我给你一个提示。有这两行之一的流浪;

if(Gdx.input.isKeyPressed(Keys.LEFT)); 
    moveCamera(-cameraSpeed, 0); 

(和你贴了同样的问题到其他行)。

+0

啊。哈哈,我的坏。 谢谢! –