Question

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();
            }
        }); 
    }
}
Was it helpful?

Solution

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

OTHER TIPS

Check out Motion Using the Keyboard. The KeyboardAnimation.java code contains a complete working example that demonstrates one way to do this.

Each instance of the KeyboardAnimation class:

  1. animates a component (JLabel) using a Timer
  2. the animation is controlled by assigned KeyStrokes
  3. a Map tracks the KeyStrokes that have been pressed to it handles multiple KeyStrokes at the same time
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top