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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…