Now I know this topic has been brought up several times, here: Swing's KeyListener and multiple keys pressed at the same time, here: How do I have multiple key inputs Listened to at the same time and here: How do I handle simultaneous key presses in Java? for instance. So I can't really expect to get any different kind of help than what's in these examples. I have also read that most of you suggest that I use KeyBinding
s instead of KeyListener
s for the situation I'm in (see below) which I now have decided to do. However I still want to know if you guys have any idea as to why my code is acting up.
So the situation, I am making a simple 2D game. I have been following thenewboston's tutorials to get started and he recommended KeyListener
s. They've been working fine for me until now when I encountered a rather weird bug.
The problem is that when I steer my spaceship up to the left it will not shoot. This only happens in this particular direction (out of the eight possible) and also if I'm already shooting I cannot steer my ship towards the top left corner.
Below is the code where I know the problem occurs. I really see no good explanation as to why though. I honestly do believe there is something wrong where my computer cannot register the up arrow, left arrow and the space bar being pressed at the same time. The ultimate proof of this then is of course that when I change the fire button from space bar to F, for instance, the code runs without issues.
//Imports
public class SpaceGame extends Core implements KeyListener{
public static void main (String[] args){
SpaceGame sg = new SpaceGame();
sg.init();
sg.run();
}
//Making all variables
private ScreenManager s;
private AffineTransform identityTransform;
private Image bg;
private Image[] shipImage = new Image[2];
private Image[] missileImage = new Image[2];
private String shipLocations[] = {/*IMAGE LOCATIONS*/};
private String missileLocations[] = {/*IMAGE LOCATIONS*/};
private Animation shipAnimation;
private Animation missileAnimation;
private Ship ship;
private ArrayList<Shot> shotList = new ArrayList<Shot>();
private boolean shooting;
private int shots = 0;
public void init(){
//Makes image objects, animations and some other stuff.
}
//This is called from the core class which has a continously running while loop in it.
public void update(long timePassed){
if(shooting){
makeShot();
}
ship.update(timePassed);
for(Shot shot : shotList){
if(shot.getAlive()){
shot.update(timePassed);
}
}
}
public void makeShot(){
//Makes shots
}
public synchronized void draw(Graphics2D g, long time) {
Window w = s.getFullScreenWindow();
w.setFocusTraversalKeysEnabled(false);
w.addKeyListener(this);
g.drawImage(bg, 0, 0, null);
for(Shot shot : shotList){
if(shot.getAlive()){
g.setTransform(shot.getTransform());
g.drawImage(shot.getImage(), Math.round(shot.getCorsClone()[0]), Math.round(shot.getCorsClone()[1]), null);
}
}
g.setTransform(ship.getTransform());
g.drawImage(ship.getImage(), Math.round(ship.getCorsClone()[0]), Math.round(ship.getCorsClone()[1]), null);
g.setTransform(identityTransform);
}
//Where the problem occurs
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_ESCAPE){
stop();
} else if(keyCode == KeyEvent.VK_UP){
ship.setDY(-1);
e.consume();
} else if(keyCode == KeyEvent.VK_RIGHT){
ship.setDX(1);
e.consume();
} else if(keyCode == KeyEvent.VK_DOWN){
ship.setDY(1);
e.consume();
} else if(keyCode == KeyEvent.VK_LEFT){
ship.setDX(-1);
e.consume();
} else if(keyCode == KeyEvent.VK_SPACE){
shooting = true;
e.consume();
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP){
ship.setDY(0);
e.consume();
} else if(keyCode == KeyEvent.VK_RIGHT){
ship.setDX(0);
e.consume();
} else if(keyCode == KeyEvent.VK_DOWN){
ship.setDY(0);
e.consume();
} else if(keyCode == KeyEvent.VK_LEFT){
ship.setDX(0);
e.consume();
} else if(keyCode == KeyEvent.VK_SPACE){
shooting = false;
e.consume();
}
}
public void keyTyped(KeyEvent e) {
e.consume();
}
}
So to conclude my question is a vague one of sorts. Is there something special with the space bar I don't know about, is there in fact a bug in my program or maybe, just maybe is there something wrong with my keyboard?
Any comments, ideas, complaints about my coding, complaints about my English, strong opinions about whether to use KeyListener
s or KeyBinding
s, code you feel like you need to see or anything at all just give me a shout and I shall try to be quick to respond.