KeyReleased无法正常工作

时间:2013-11-12 00:54:00

标签: java swing awt japplet keyevent

进行JApplet飞机游戏,不确定为什么我的keyReleased未被识别。我初始化飞机类中的布尔值,这是飞机的更新方法。它无法识别何时释放键,因此一旦被击中一次,布尔值始终保持为真。

public void update(Observable obj, Object arg) {

        gm1942.GameEvents ge = (gm1942.GameEvents) arg;
        if(ge.type == 1) {
            KeyEvent e = (KeyEvent) ge.event;

        if(e.getKeyCode() == KeyEvent.VK_LEFT){
                System.out.println("Left");
                if (e.getID() == KeyEvent.KEY_RELEASED){
                    System.out.println("left released");
                    left = false;
                }
                if (e.getID() == KeyEvent.KEY_PRESSED){
                    left = true;
                }
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                System.out.println("Right");
                if (e.getID() == KeyEvent.KEY_RELEASED){
                    right = false;
                }else if (e.getID() == KeyEvent.KEY_PRESSED){
                    right = true;
                }
            }
            if(e.getKeyCode() == KeyEvent.VK_UP){
                System.out.println("Up");
                if (e.getID() == KeyEvent.KEY_RELEASED){
                    up = false;
                }else if (e.getID() == KeyEvent.KEY_PRESSED){
                    up = true;
                }
            }
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
                System.out.println("Down");
                if (e.getID() == KeyEvent.KEY_RELEASED){
                    down = false;
                }else if (e.getID() == KeyEvent.KEY_PRESSED){
                    down = true;
                }
            }
            if(left == true && x!= 0){
                x -= speed;
            }
            if(right == true && x!= 570){
                x += speed;
            }
            if(up == true && y!= 0){
                y -= speed;
            }
            if(down == true && y!= 390){
                y += speed;
            }


        }

0 个答案:

没有答案