1
我想要编程游戏,有2个玩家。其中一个玩(w,a,s,d)另一个玩游戏bye(y,g,h,j)。 但是如果一个玩家保持关键,那么keylistner不能听其他的一个键。我应该怎么做才能让他们两个都能听呢?通过keyListner获得多键
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 39)
player1.setBounds((dim_player1.width += 5), dim_player1.height, 80, 120);
else if (e.getKeyCode() == 37)
player1.setBounds((dim_player1.width -= 5), dim_player1.height, 80, 120);
else if (e.getKeyCode() == 40)
player1.setBounds(dim_player1.width, (dim_player1.height += 5), 80, 120);
else if (e.getKeyCode() == 38)
player1.setBounds(dim_player1.width, (dim_player1.height -= 5), 80, 120);
}
这是我简单的代码,我想,当玩家按下两个38 + 39 e.g它做他们两个,但我不明白我应该怎么办。
[Swing's KeyListener和多个按键同时按下]的可能重复。(http://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-时间) –
我看到了,但我不明白我该怎么做 if(pressed.size()> 1)//当前有多个键被按下。 //重复按按钮获得按键。 } 例如我想要getKeyChar这两个键,我该怎么做呢? –