简体   繁体   English

如何使多个按键绑定同时工作?

[英]How can i make multiple Key Bindings work at the same time?

I need to design a game with two players. 我需要设计一个有两个玩家的游戏。 Each has a ball and should be able to move the ball to right or left, the first player with 'a' 'd' buttons and the second player with right,left arrow buttons. 每个人都有一个球,应该能够左右移动球,第一个球员带有'a''d'按钮,第二个球员带有左右箭头按钮。 However currently one player needs to wait for the other player's action to be completed in order to move their own ball. 但是,当前一名球员需要等待另一名球员的动作完成才能移动自己的球。 How can i resolve that problem? 我该如何解决这个问题? Here is the related parts of my code: 这是我的代码的相关部分:

    public class AnimationWindow extends JPanel{

      public AnimationWindow()
        {

            super();
            ....
            ....
            cezmiMover();

        } 



public void cezmiMover(){

        this.getInputMap().put(KeyStroke.getKeyStroke('a'), "left1");
        this.getActionMap().put("left1", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {

                board.cezmi1.moveLeft();
            }
        });

        this.getInputMap().put(KeyStroke.getKeyStroke('d'), "right1");
        this.getActionMap().put("right1", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {

                board.cezmi1.moveRight();
            }
        });

        this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "left2");
        this.getActionMap().put("left2", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {

                board.cezmi2.moveLeft();
            }
        });

        this.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "right2");
        this.getActionMap().put("right2", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {

                board.cezmi2.moveRight();
            }
        }); 
    }
}

You need to use a series of flags and some kind of "update" loop to update the state of the game depending on the state of the flags... 您需要使用一系列标志和某种“更新”循环来根据标志的状态来更新游戏的状态...

For example, start by creating a series of flags... 例如,首先创建一系列标志...

private boolean p1Left, p1Right, p2Left, p2Right = false;

These could just as easily be maintained by the individual player objects, but you've not provided that much code... 这些可以由单个播放器对象轻松维护,但是您没有提供那么多代码...

Next, you need to monitor for key press and key release events and set the state of the flag as required... 接下来,您需要监视按键和按键释放事件,并根据需要设置标志的状态...

this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "right1down");
this.getActionMap().put("right1down", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        p1Right = true;
    }
});

this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "right1up");
this.getActionMap().put("right1up", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        p1Right = false;
    }
});

Then you need some kind loop or timer that can update the state of the game. 然后,您需要某种可以更新游戏状态的循环或计时器。 Personally, I like using javax.swing.Timer , but that's just me. 就个人而言,我喜欢使用javax.swing.Timer ,但这就是我。

On each run of the update loop, you need to check the state of each flag and update the objects accordingly... 在更新循环的每次运行中,您需要检查每个标志的状态并相应地更新对象。

if (p1Right) {
    board.cezmi1.moveRight();
}

For example 举个例子

Check out Motion Using the Keyboard . 使用键盘检查动作 The KeyboardAnimation.java code contains a complete working example that demonstrates one way to do this. KeyboardAnimation.java代码包含一个完整的工作示例,该示例演示了执行此操作的一种方法。

Each instance of the KeyboardAnimation class: KeyboardAnimation类的每个实例:

  1. animates a component (JLabel) using a Timer 使用Timer对组件(JLabel)进行动画处理
  2. the animation is controlled by assigned KeyStrokes 动画由分配的KeyStrokes控制
  3. a Map tracks the KeyStrokes that have been pressed to it handles multiple KeyStrokes at the same time 一个地图跟踪已​​被按下的按键,可以同时处理多个按键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM