I'm using key bindings to listen for keys, the program is just listening for the left and right arrow to move an image (I'm making Space Invaders). However, there is a slight delay in responding which I would like to get rid of. I've been reading up on this but have only see a lot of vague answers. A lot of people seem to say to set a boolean for when a key is pressed or released and listen for it in the game loop, and act according to the boolean value. I've tried this but I'm getting the same results. Any suggestions or examples on how to fix this problem?
Here's my code
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);
}
}
}
Here is the tank class
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;
}
}
One other thing I should mention, I'm pretty sure the delay is the OS from what I've read, I'm not positive. The delay doesn't happen when you press one key. Or, if you're holding right and then press left, it will move left with no delay. It seems the delay only occurs when you are holding one direction and release that and press the other direction at about the same time that this delay occurs.
If anybody has any suggestions it would be appreciated. Thanks