有人可以解释一下吗? (运动不正常)

时间:2016-04-15 12:21:23

标签: java libgdx

我在游戏中添加了一个东西,所以当你按下空格时你会得到一个小的速度提升。但是,它只能在您使用WASD密钥时正常工作。如果按向上和向左或向下和向右箭头键,增强功能将无法工作,但否则会有效。任何人都可以解释为什么会这样做吗?

以下是代码:

    boostTimer += Gdx.graphics.getDeltaTime();

    if(boostTimer > 3){
    snailBoost = 0;
    boostTimer = 0;
    }

    //movement
    if(Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A)){

        if(Gdx.input.isKeyPressed(Keys.SPACE) && snailBoost < 20){
            snailBoost++;
            snail.getBounds().x -= (snailSpeed + 100) * delta;
        }

        //flips the sprite
        snail.getSprite().setFlip(false, true);
        snail.getBounds().x -= snailSpeed * delta;

        //handles what happens when you go outside the game world
        if(snail.getBounds().x < -100)snail.getBounds().x = 1920;
    }

    if(Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D)){

        if(Gdx.input.isKeyPressed(Keys.SPACE) && snailBoost < 20){
            snailBoost++;
            snail.getBounds().x += (snailSpeed + 100) * delta;
        }

        snail.getSprite().setFlip(true, true);
        snail.getBounds().x += snailSpeed * delta;

        if(snail.getBounds().x > 2020)snail.getBounds().x = -100;
    }

    if(Gdx.input.isKeyPressed(Keys.UP) || Gdx.input.isKeyPressed(Keys.W)){

        if(Gdx.input.isKeyPressed(Keys.SPACE) && snailBoost < 20){
            snailBoost++;
            snail.getBounds().y -= (snailSpeed + 100) * delta;
        }

        snail.getBounds().y -= snailSpeed * delta;

        if(snail.getBounds().y < -100)snail.getBounds().y = 1080;
    }

    if(Gdx.input.isKeyPressed(Keys.DOWN) || Gdx.input.isKeyPressed(Keys.S)){

        if(Gdx.input.isKeyPressed(Keys.SPACE) && snailBoost < 20){
            snailBoost++;
            snail.getBounds().y += (snailSpeed + 100) * delta;
        }

        snail.getBounds().y += snailSpeed * delta;

        if(snail.getBounds().y > 1080)snail.getBounds().y = -100;
    }

对不起杂乱的代码。 :P 游戏开发还是新手。

1 个答案:

答案 0 :(得分:2)

便宜的键盘共享按键的控制线,因此某些按键组合无法正确注册,并且一次按下3-4个以上的按键将不会发生任何事情。

要么购买更昂贵的键盘,要么选择按键以便它们工作(例如您已经注意到的着名WASD作品)。与代码无关。