我在编程的游戏中有以下代码:
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
doIShoot = false;
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
shoot();
}
}
public void shoot(){
if(!doIShoot){
doIShoot = true;
// code that creates bullet
}
}
玩家必须按下每个子弹的按钮,当他占据空间时不应自动拍摄。 在Windows中运行得很好,但在Ubuntu Linux中却没有。 似乎当我持有空格时,它将始终交替执行keyReleased和keyPressed。
为什么Ubuntu会这样做,我怎样才能让它发挥作用?
我刚发现:How to stop repeated keyPressed() / keyReleased() events in Swing
解决方案:http://brunez.net63.net/tutorials/keypressfix/keypressfix.php