如何将 Key Binding 方法移动到它自己的 Class 中?

How to move Key Binding method into its own Class?

我正在制作游戏并且在我的 GamePanel class 中设置了键绑定。但是我希望能够将我的键绑定方法放在它自己单独的 class 中,以便游戏的其他面板可以访问该方法,这样我就不必每次都重写它。如您所见,我已经将它移了过来,但是我在调​​用 addKeyBinding 方法的行上遇到了一个空指针异常。我在想这与不知道调用什么动作有关?但我不确定。我该如何解决这个问题?

游戏面板class:


public class GamePanel extends JPanel implements ActionListener {
    // Global Variables
    private Ball ball;
    private Paddle paddle;
    private GameFrame gameFrame;
    private int width;
    private int height; 
    private Timer timer;
    private int brickHeight = 30;
    private int brickWidth = 60;
    ArrayList<Bricks> brickArray = new ArrayList<Bricks>();
    int brickCount;
    int leftOverSpace;
    private int ballXDir = -1;
    private int ballYDir = -2;
    private boolean play = false;
    private int delay = 8;
    private KeyBindings kBindings;

    // create a constructor
    GamePanel (int gamePanelWidth, int gamePanelHeight) {
        this.setWidth(gamePanelWidth);
        this.setHeight(gamePanelHeight);
        initialiseGame();
        this.isVisible();
    }

    private void initialiseGame() {
        play = false;
        ball = new Ball(10, 520, 30, 30, this); //create the ball object
        paddle = new Paddle(this, 50, 700, 100, 10); //creates paddle object
        initialiseBrickArray();
        
        //initialise key bindings for space, left and right
        kBindings.addKeyBinding(this, KeyEvent.VK_SPACE, "startBall", (evt) -> {
            play = true;
        });
        kBindings.addKeyBinding(this, KeyEvent.VK_LEFT, "moveLeft", (evt) -> {
            paddle.moveLeft();
        });
        kBindings.addKeyBinding(this, KeyEvent.VK_RIGHT, "moveRight", (evt) -> {
            paddle.moveRight();
        });       
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
    }  
}

我的新 KeyBindings class:

public class KeyBindings implements ActionListener {

    public void addKeyBinding(JComponent comp, int keyCode, String id, ActionListener actionListener) {
        InputMap im = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap ap = comp.getActionMap();

        im.put(KeyStroke.getKeyStroke(keyCode, 0, false), id);
        ap.put(id, new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                actionListener.actionPerformed(e);
            }
        });

        return;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
    
}

您应该在构造函数或其他地方初始化 kBindings,但在调用 kBindings.addKeyBinding 之前。刚刚

kBindings = new KeyBindings();

首先,KeyBindings class 不需要实现 ActionListener。您实际上并没有将此 class 的实例作为 ActionListener 添加到按钮。这只是一个 class 和一个方便的方法。

您实际上并没有更改 KeyBindings class 的任何 属性。所以这可能只是 class:

的静态方法
//public void addKeyBinding(...)
public static void addKeyBinding(...)

然后调用它:

//kBindings.addKeyBinding(...);
KeyBindings.addKeyBinding(...);