Java - KeyListener多个密钥

时间:2016-08-09 10:00:26

标签: java keylistener

现在我知道这个主题已多次出现,例如:Swing's KeyListener and multiple keys pressed at the same time,此处:How do I have multiple key inputs Listened to at the same time和此处:How do I handle simultaneous key presses in Java?。所以我真的不希望获得任何不同的帮助,而不是这些例子中的帮助。我还读到大多数人都建议我使用KeyBinding而不是KeyListener来处理我现在决定要做的情况(见下文)。但是我仍然想知道你们是否知道为什么我的代码会起作用。

所以情况,我正在做一个简单的2D游戏。我一直在关注新波士顿的教程开始,他推荐KeyListener。直到现在,当我遇到一个相当奇怪的错误时,他们一直在为我工作。

问题在于,当我将我的太空船引导到左侧时,它不会射击。这只发生在这个特定的方向上(在可能的八个方向中),如果我已经开枪,我无法将我的船转向左上角。

以下是我知道问题发生的代码。我真的没有看到为什么这么好的解释。老实说,我确实认为我的电脑无法同时注册向上箭头,向左箭头和空格键时出现问题。当然,当我将火警按钮从空格键更改为F时,最终证明了这一点,代码运行没有问题。

//Imports

public class SpaceGame extends Core implements KeyListener{
    public static void main (String[] args){
        SpaceGame sg = new SpaceGame();
        sg.init();
        sg.run();
    }

    //Making all variables
private ScreenManager s;
private AffineTransform identityTransform;
private Image bg;
private Image[] shipImage = new Image[2];
private Image[] missileImage = new Image[2];
private String shipLocations[] = {/*IMAGE LOCATIONS*/};
private String missileLocations[] = {/*IMAGE LOCATIONS*/};
private Animation shipAnimation;
private Animation missileAnimation;
private Ship ship;
private ArrayList<Shot> shotList = new ArrayList<Shot>();
private boolean shooting;
private int shots = 0;

    public void init(){
        //Makes image objects, animations and some other stuff.
    }

    //This is called from the core class which has a continously running while loop in it.
    public void update(long timePassed){
        if(shooting){
            makeShot();
        }
        ship.update(timePassed);
        for(Shot shot : shotList){
            if(shot.getAlive()){
            shot.update(timePassed);
        }
    }
}

public void makeShot(){
    //Makes shots
}

public synchronized void draw(Graphics2D g, long time) {
    Window w = s.getFullScreenWindow();
    w.setFocusTraversalKeysEnabled(false);
    w.addKeyListener(this);
    g.drawImage(bg, 0, 0, null);
    for(Shot shot : shotList){
        if(shot.getAlive()){
            g.setTransform(shot.getTransform());
            g.drawImage(shot.getImage(), Math.round(shot.getCorsClone()[0]), Math.round(shot.getCorsClone()[1]), null);
        }
    }
    g.setTransform(ship.getTransform());
    g.drawImage(ship.getImage(), Math.round(ship.getCorsClone()[0]), Math.round(ship.getCorsClone()[1]), null);
    g.setTransform(identityTransform);
}


//Where the problem occurs
public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_ESCAPE){
        stop();
    } else if(keyCode == KeyEvent.VK_UP){
        ship.setDY(-1);
        e.consume();
    } else if(keyCode == KeyEvent.VK_RIGHT){
        ship.setDX(1);
        e.consume();
    } else if(keyCode == KeyEvent.VK_DOWN){
        ship.setDY(1);
        e.consume();
    } else if(keyCode == KeyEvent.VK_LEFT){
        ship.setDX(-1);
        e.consume();
    } else if(keyCode == KeyEvent.VK_SPACE){
        shooting = true;
        e.consume();
    }
}

    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_UP){
            ship.setDY(0);
            e.consume();
        } else if(keyCode == KeyEvent.VK_RIGHT){
            ship.setDX(0);
            e.consume();
        } else if(keyCode == KeyEvent.VK_DOWN){
            ship.setDY(0);
            e.consume();
        } else if(keyCode == KeyEvent.VK_LEFT){
            ship.setDX(0);
            e.consume();
        } else if(keyCode == KeyEvent.VK_SPACE){
            shooting = false;
            e.consume();
        } 
    }

    public void keyTyped(KeyEvent e) {
        e.consume();
    }
}

总而言之,我的问题是一种模糊的问题。我不知道的空格键是否有什么特别之处,实际上我的程序中是否有错误,或者也许,我的键盘可能有问题?

有关我的编码的任何评论,想法,投诉,有关我的英语的投诉,关于是否使用KeyListenerKeyBinding的强烈意见,您认为需要查看的代码或任何内容只是给我一个喊叫,我会尽快回应。

1 个答案:

答案 0 :(得分:2)

很多人都有这个问题。它被称为Keyboard Ghosting,意味着某些组合根本不适用于便宜的键盘。这就是为什么存在高级游戏键盘的原因。所以你的代码没有错。无法优化。您需要使用有效的组合键。
为了证明我的答案,这里有一些链接。