我正在使用键绑定来侦听键,程序只是在侦听左右箭头来移动图像(我正在制作太空入侵者)。但是,我想要摆脱的回应略有延迟。我一直在读这个,但只看到很多模糊的答案。许多人似乎都说要在按下或释放按键时设置一个布尔值,并在游戏循环中监听它,并根据布尔值进行操作。我试过这个,但我得到了相同的结果。有关如何解决此问题的任何建议或示例?
这是我的代码
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;
}
}
另外一件事我应该提一下,我很确定延迟是我读过的操作系统,我不是正面的。按一个键时不会发生延迟。或者,如果你按住右键然后向左按,它将向左移动而没有延迟。似乎延迟仅发生在您按住一个方向并释放该方向并在此延迟发生的同一时间按另一个方向时。
如果有人有任何建议,我们将不胜感激。 谢谢