我需要設計一個雙人遊戲。每個球都有球,應該能夠將球移動到右或左,第一個球員有'a''d'按鈕,第二個球員有右,左箭頭按鈕。然而,目前一名球員需要等待另一名球員的動作完成才能移動自己的球。我如何解決這個問題?這裏是我的代碼的相關部分:我怎樣才能讓多個鍵綁定同時工作?
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();
}
});
}
}