Java 关键版本没有被拾取?

Java key releases not being picked up?

我是 java 编程的新手,并且已经开始编写游戏。我有一个随 wasd 一起移动的角色,但发现当:

  1. 移动键被按下。
  2. JFrame点击离开。 (所以它变得不专心。)
  3. 释放相同的键。
  4. JFrame 再次获得焦点。

...游戏不会发现玩家不应该移动。所以我试图通过将所有键数组更改为 false 来修复它:

public class KeyManager implements KeyListener {

private Game game;

private boolean[] keys;
public boolean up, down, left, right; //Player class reads these variables

public KeyManager(Game game) {
    this.game = game;
    keys = new boolean[256];
}

public void tick() {
    if(!game.getDisplay().getFrame().isFocused()) { //Here is the problem
        for(int i = 0; i < keys.length; i++) {
            keys[i] = false; //Repeats 256 x 60 times a second
        }
    }
    up = keys[KeyEvent.VK_W];    //I realize that I could just change
    down = keys[KeyEvent.VK_S];  //up, down, left, right to false but
    left = keys[KeyEvent.VK_A];  //when the JFrame is refocused the game
    right = keys[KeyEvent.VK_D]; //still doesn't know the key was released
}

@Override
public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;
}

@Override
public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
}

@Override
public void keyTyped(KeyEvent e) {

}

}

它起作用了,但在仔细考虑代码后我意识到,由于每秒调用 tick() 方法 60 次并且键数组有 256 个键长,它每秒更新 15,900 个键布尔值,仅用于 4 个重要的需要更改为 false 的键。

最有效的方法是什么?我觉得我错过了一些非常简单的东西。

我要做的第一件事是使用 Key Bindings API 而不是 KeyListener。我会让它独立于 "game loop".

然后我会在 JFrame 上使用 FocusListener 并在焦点为 lost/gained 时简单地 pause/resume "game loop",可选择重置键标志

类似这样的东西,例如...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                TestPane tp = new TestPane();
                frame.addFocusListener(new FocusListener() {
                    @Override
                    public void focusGained(FocusEvent e) {
                        tp.resume();
                    }

                    @Override
                    public void focusLost(FocusEvent e) {
                        tp.pause(true);
                    }
                });
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private boolean aPressed = false;
        private Timer timer;

        public TestPane() {
            InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap actionMap = getActionMap();

            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "A.pressed");
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "A.released");
            actionMap.put("A.pressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    aPressed = true;
                }
            });
            actionMap.put("A.released", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    aPressed = false;
                }
            });

            timer = new Timer(16, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("A pressed = " + aPressed);
                }
            });
        }

        public void resume() {
            timer.restart();
        }

        public void pause(boolean reset) {
            timer.stop();
            if (reset) {
                aPressed = false;
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}