(I admit to have posted this also in another forum, but I didn't yet get help there and I'm in 'dire' need to solve this problem. Help would be much appreciated. Thank you).
In my Java 2D game, two tanks are controlled as follows:
Tank 1
- Move forward: UP arrow
- Adjust angle of movement: LEFT and RIGHT arrows
Tank 2
- Move forward: W key
- Adjust angle of movement: A and D keys
It is possible to adjust the angle of a tank while it's moving forward (e.g., striking the left arrow or the right arrow while striking the up arrow). It's also possible to move both tanks forward simultaneously.
However, while one tank is moving forward and changing its angle (e.g., striking the up and the left/right key at the same time), the other tank is only able to move forward. That is to say, it cannot adjust its angle (e.g., striking W and A at the same time won't work, while the other tank is doing the same thing; the A key is ignored).
Why is that? Here's the relevant code:
In the Board class, that has most of the game logic:
while( game_is_running ) { keysPressed1 = tank1.getKeys(); keysPressed2 = tank2.getKeys(); if(keysPressed1[0]==true)tank1.setAngle(tank1.getAngle()-3); if(keysPressed1[1]==true)tank1.setAngle(tank1.getAngle()+3); if(keysPressed1[2]==true){ tank1.setDX(2 * Math.cos(Math.toRadians(tank1.getAngle()))); tank1.setDY(2 * Math.sin(Math.toRadians(tank1.getAngle()))); } if(keysPressed1[2]==false){ tank1.setDX(0); tank1.setDY(0); } tank1.move(); if(keysPressed2[0]==true)tank2.setAngle(tank2.getAngle()-3); if(keysPressed2[1]==true)tank2.setAngle(tank2.getAngle()+3); if(keysPressed2[2]==true){ tank2.setDX( (-1) * ( 2 * Math.cos(Math.toRadians(tank2.getAngle()) ) )); tank2.setDY( (-1) * (2 * Math.sin(Math.toRadians(tank2.getAngle()) ) )); } if(keysPressed2[2]==false){ tank2.setDX(0); tank2.setDY(0); } tank2.move(); repaint(); }
In the Tank class:
public class Tank extends Entity { public Tank(String type){ this.type = type; if(type=="red"){ image = new ImageIcon(this.getClass().getResource("sprites/redtank1.PNG")).getImage(); x = 200 - image.getWidth(null); y = 400; } if(type=="blue"){ image = new ImageIcon(this.getClass().getResource("sprites/bluetank1.PNG")).getImage(); x = 850; y = 400; } width = image.getWidth(null); height = image.getHeight(null); angle = 0; dx = 0; dy = 0; keysPressed = new boolean[3]; } public void keyPressed(KeyEvent e){ int key = e.getKeyCode(); if(key == KeyEvent.VK_LEFT){ keysPressed[0] = true; } if(key == KeyEvent.VK_RIGHT){ keysPressed[1] = true; } if(key == KeyEvent.VK_UP){ keysPressed[2] = true; } if(key == KeyEvent.VK_A){ keysPressed[0] = true; } if(key == KeyEvent.VK_D){ keysPressed[1] = true; } if(key == KeyEvent.VK_W){ keysPressed[2] = true; } } public void keyReleased(KeyEvent e){ int key = e.getKeyCode(); if(key == KeyEvent.VK_LEFT){ keysPressed[0] = false; } if(key == KeyEvent.VK_RIGHT){ keysPressed[1] = false; } if(key == KeyEvent.VK_UP){ keysPressed[2] = false; } if(key == KeyEvent.VK_A){ keysPressed[0] = false; } if(key == KeyEvent.VK_D){ keysPressed[1] = false; } if(key == KeyEvent.VK_W){ keysPressed[2] = false; } } }
You will probably suggest I use Key Bindings, but is there a way to still use KeyListener and have all buttons work at the same time without errors?
Thanks