hello,
am facing a little problem here,
i have a square that move with arrow key (up = forward, down = backward, left & right = +/- angle )
and shoot with Space key
the thing is that if for example i want to move forward,rotate left and shoot in the same time, it just doesn’t work, it only take the 2 first pressed key (in this case up & left) and ignore the Space key
here is the lines in the Board class about the keyListener (please tell me if you need the full class )
private boolean moveForward, moveBackward, left, right, fire;
-
-
addKeyListener(new Controll());
-
-
fire = left = right = moveForward = moveBackward = false;
-
-
-
// check if shooting
if (fire) {
hero.fire(10, 3);
}
// moving the hero
if (moveForward) {
hero.moveForward(speed);
}
if (moveBackward) {
hero.moveBackword(speed);
}
if (left) {
tmpAngle -= 1;
}
if (right) {
tmpAngle += 1;
}
-
-
-
// game key controll
private class Controll extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_UP) {
moveForward = true;
}
if (e.getKeyCode() == e.VK_DOWN) {
moveBackward = true;
}
if (e.getKeyCode() == e.VK_LEFT) {
left = true;
}
if (e.getKeyCode() == e.VK_RIGHT) {
right = true;
}
if (e.getKeyCode() == e.VK_SPACE) {
fire = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == e.VK_UP) {
moveForward = false;
}
if (e.getKeyCode() == e.VK_DOWN) {
moveBackward = false;
}
if (e.getKeyCode() == e.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == e.VK_RIGHT) {
right = false;
}
if (e.getKeyCode() == e.VK_SPACE) {
fire = false;
}
}
}
thank you