1
public void buttons(){
     int c = WHEN_IN_FOCUSED_WINDOW;

        Action right = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                player.setVX(2);
            }
        };
        Action stop = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                player.setVX(0);
                player.setVY(0);
            }
        };

        Action up = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                player.setVY(-2);
            }
        };
           getInputMap(c).put(KeyStroke.getKeyStroke("D"), "pressed");
           getActionMap().put("pressed", right);
           getInputMap(c).put(KeyStroke.getKeyStroke("released D"), "released");
           getActionMap().put("released", stop);
           getInputMap(c).put(KeyStroke.getKeyStroke("W"), "pressed");
           getActionMap().put("pressed", up);
           getInputMap(c).put(KeyStroke.getKeyStroke("released W"), "released");
           getActionMap().put("released", stop);

 }

Why is that when I press W or D it goes up...

What is the problem?

D should go right

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Are you sure it's the key bindings and not the `setVX` method? Add a debug statement to each of the `actionPerformed` methods to see if pressing `D` is actually calling `setVX` (via the `actionPerformed` method) – MadProgrammer Feb 27 '13 at 03:50
  • yea my setVX is right but when I get rid of my setVY actionperformed – user2113792 Feb 27 '13 at 04:01
  • my VX work.. i dont know why it is working when I git rid of my VY ation performed – user2113792 Feb 27 '13 at 04:01
  • Try adding debug statements to each of `actionPerformed` methods and see what's been called and in what order. I think it might have something to do with your `stop` action – MadProgrammer Feb 27 '13 at 04:03
  • I dont know something wrong with the actionperformed.. maybe it only accept two actionperform not 3 – user2113792 Feb 27 '13 at 04:13

1 Answers1

4

You are overwriting the value in your action map because you are using the same action name "pressed" for both the up and right action.

getInputMap(c).put(KeyStroke.getKeyStroke("D"), "pressed");
getActionMap().put("pressed", right);
getInputMap(c).put(KeyStroke.getKeyStroke("released D"), "released");
getActionMap().put("released", stop);
getInputMap(c).put(KeyStroke.getKeyStroke("W"), "pressed");
getActionMap().put("pressed", up); // this overwrites the "pressed" action name above with the up action
getInputMap(c).put(KeyStroke.getKeyStroke("released W"), "released");
getActionMap().put("released", stop); // similarly, this is redundant because you have the same thing above

The following should fix it:

getInputMap(c).put(KeyStroke.getKeyStroke("D"), "right");
getInputMap(c).put(KeyStroke.getKeyStroke("released D"), "stop");
getInputMap(c).put(KeyStroke.getKeyStroke("W"), "up");
getInputMap(c).put(KeyStroke.getKeyStroke("released W"), "stop");
getActionMap().put("right", right);
getActionMap().put("up", up);
getActionMap().put("stop", stop);
squawknull
  • 5,131
  • 2
  • 17
  • 27