Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

java - 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. 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();
            }
        }); 
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...