我正在使用键绑定来监听键,程序只是监听左右箭头来移动图像(我正在制作 Space Invaders)。但是,我想摆脱响应的轻微延迟。我一直在阅读此内容,但只看到很多模糊的答案。很多人似乎说要为按下或释放键时设置一个布尔值,并在游戏循环中监听它,并根据布尔值进行操作。我已经尝试过了,但我得到了相同的结果。有关如何解决此问题的任何建议或示例?
这是我的代码
public class Board extends JPanel implements ActionListener{
Timer timer;
Tank tank;
MoveRight moveRight;
MoveLeft moveLeft;
releaseMoveRight releasemoveRight;
releaseMoveLeft releasemoveLeft;
int tankdx = 0;
boolean rightKeyDown;
boolean leftKeyDown;
boolean boardset;
public Board(){
setBackground(Color.BLACK);
ImageIcon alien1ii = new ImageIcon(
this.getClass().getResource("si_Alien1.png"));
Image alien1 = alien1ii.getImage();
ImageIcon alien2ii = new ImageIcon(
this.getClass().getResource("si_Alien2.png"));
Image alien2 = alien2ii.getImage();
ImageIcon alien3ii = new ImageIcon(
this.getClass().getResource("si_Alien3.png"));
Image alien3 = alien3ii.getImage();
timer = new Timer(5, this);
timer.start();
tank = new Tank();
moveRight = new MoveRight();
moveLeft = new MoveLeft();
releasemoveRight = new releaseMoveRight();
releasemoveLeft = new releaseMoveLeft();
// keys--------------------------------------
// Right pressed
// panel is the JPanel, .put(define key,
// followed by String doEnterAction, used to
// pair to AbstractAction
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("RIGHT"), "moveRight");
//pairs String pair from input map with abstract action
getActionMap().put("moveRight", moveRight);
//Right released
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("released RIGHT"), "releasemoveRight");
getActionMap().put("releasemoveRight", releasemoveRight);
//Left pressed
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("LEFT"), "moveLeft");
getActionMap().put("moveLeft", moveLeft);
//Left released
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("released LEFT"), "releasemoveLeft");
getActionMap().put("releasemoveLeft", releasemoveLeft);
//Button in Game----------------------------------------------
JButton button = new JButton(new AbstractAction("hello2"){
@Override
public void actionPerformed(ActionEvent e){
boardset = false;
}
});
this.add(button);
//actual game (kinda)-----------------------------------------
setFocusable(true);
setDoubleBuffered(true);
}
//draw stuff------------------------------------------------------
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.WHITE);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(tank.getTank(), tank.getx(), tank.getY(), this);
g2d.drawLine(0, (tank.getY()+25), 400, (tank.getY()+25));
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void setBoardset(boolean x){
boardset = x;
}
public boolean getBoardset(){
return boardset;
}
//keybinding actions------------------------------------------
public class MoveRight extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
rightKeyDown = true;
tankdx = 1;
}
}
public class releaseMoveRight extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
rightKeyDown = false;
tankdx = 0;
}
}
public class MoveLeft extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
leftKeyDown = true;
tankdx = -1;
}
}
public class releaseMoveLeft extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
leftKeyDown = false;
tankdx = 0;
}
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
if(leftKeyDown || rightKeyDown){
tank.move(tankdx);
}
}
}
这是坦克类
public class Tank {
int x = 180;
int y = 320;
int dx;
int tankSpeed;
ImageIcon tankii = new ImageIcon(this.getClass().getResource("si_Tank.png"));
Image tank = tankii.getImage();
public Image getTank(){
return tank;
}
public int getx(){
return x;
}
public int getY(){
return y;
}
public void move(int dx){
x += dx;
}
}
我应该提到的另一件事,我很确定延迟是我读过的操作系统,我不肯定。按一个键时不会发生延迟。或者,如果您按住右然后按左,它将立即向左移动。似乎只有在您按住一个方向并释放该方向并在此延迟发生的大约同时按下另一个方向时才会发生延迟。
如果有人有任何建议,将不胜感激。谢谢