Java Keylistener слушает тот же keyEvent - PullRequest
0 голосов
/ 09 сентября 2018

Здравствуйте, у меня проблема с Java KeyListener. Я делаю игру, в которой объект движется при нажатии клавиш со стрелками. Я установил задержку для каждого KeyEvent (чтобы настроить время аудиоклипа). Когда я нажимаю правую клавишу и нажимаю любую другой сразу после, KeyListener прослушивает тот же KeyEvent. Вот код для Правого Ключа:

if (k.getKeyCode() == KeyEvent.VK_RIGHT && !k.isConsumed()) {
        k.consume();
        k=null;
        int flag;
        flag = 0;
        ox = obj.getX();
        oy = obj.getY();
        ox += 5;
        Checkwin();
        System.out.println(ox+" "+oy);
        for (int i = 20; i <= 150; i++) {
            if (oy == i && ox == 15 ) {
                 System.out.println("Here right 1");
                 //try{Thread.sleep(500);}catch(Exception e){e.printStackTrace();}
                Checklose(lives);
                 lives-=1;
            livesl.setText("LIVES: "+Integer.toString(lives));
                 try{
               loss=AudioSystem.getAudioInputStream(new File("T:\\Watchout\\loss.wav"));
               clip2.open(loss);
               clip2.start();
               ox=0;oy=20;

               Thread.sleep(DELAY);
           }catch(Exception e){}
                obj.setBounds(ox, oy, 50, 50);
                flag = 1;
                return;
            }
        }
        for (int i = 460; i >= 300; i--) {
            if (oy == i && ox >= 15 && ox < 20) {
                Checklose(lives);
                 System.out.println("Here right 2");
                lives-=1;
            livesl.setText("LIVES: "+Integer.toString(lives));
                try{
               loss=AudioSystem.getAudioInputStream(new File("T:\\Watchout\\loss.wav"));
               clip2.open(loss);
               clip2.start();
               ox=0;oy=20;
               Thread.sleep(DELAY);
           }catch(Exception e){}
                obj.setBounds(ox, oy, 50, 50);
                flag = 1;
                return;
            }
        }
        for (int i = 150; i < 300; i++) {
            if (oy == i && ox >= 110 && ox < 120) {
                 System.out.println("Here right 3");
                Checklose(lives);
                lives-=1;
            livesl.setText("LIVES: "+Integer.toString(lives));
                try{
               loss=AudioSystem.getAudioInputStream(new File("T:\\Watchout\\loss.wav"));
               clip2.open(loss);
               clip2.start();
               ox=0;oy=20;
               Thread.sleep(DELAY);
           }catch(Exception e){}
                obj.setBounds(ox, oy, 50, 50);
                flag = 1;
                return;
            }
        }
        for (int i = 0; i <= 80; i++) {
            if (oy == i && ox >= 310 && ox < 320) {
                Checklose(lives);
                 System.out.println("Here right 4");
                lives-=1;
            livesl.setText("LIVES: "+Integer.toString(lives));
                try{
               loss=AudioSystem.getAudioInputStream(new File("T:\\Watchout\\loss.wav"));
               clip2.open(loss);
               clip2.start();
               ox=0;oy=20;
               Thread.sleep(DELAY);
           }catch(Exception e){}

                obj.setBounds(ox, oy, 50, 50);
                flag = 1;
                return;
            }
        }
        for (int i = 160; i <= 460; i++) {
            if (oy == i && ox >= 310 && ox < 320) {
                Checklose(lives);
                 System.out.println("Here right 5");
                lives-=1;
            livesl.setText("LIVES: "+Integer.toString(lives));
                try{
               loss=AudioSystem.getAudioInputStream(new File("T:\\Watchout\\loss.wav"));
               clip2.open(loss);
               clip2.start();
               ox=0;oy=20;
               Thread.sleep(DELAY);
           }catch(Exception e){}
                obj.setBounds(ox,oy, 50, 50);
                flag = 1;
                return;
            }
        }
        if (flag != 1) {
            for(int i=20;i<=440;i++)
            {if(oy==i && ox>=440)
                return;}
            obj.setBounds(ox, oy, 50, 50);
        }
        clip2.close();
    }
}
...