I have posted this in another forum. I know 'croos-posting' isn't well-accepted here, but I didn't get help there for a long while and would very much like to solve this problem. Thank you for understanding.
I'm working on a Java 2D game which requires a max of six keys be held down at the same time.
The game is for two players on the same keyboard, playing simultaneously.
However, all three computers I ran the program on only allow a max of three keys held at a time. They all have trouble with reacting to more than three keys being held.
It seems that pressing a new key after three are already held, either cancels some other key-holding or is ignored.
I've been told that this is a hardware issue. Most keyboards can't handle more than three keys held at a time. But a lot of games do require this, and they do not require special gaming-keyboards to run on my computer without problems.
So there has to be a solution that will make the game playable on any standard keyboard.
If there is, could you please explain to me how to code it in my program?
(I use Key Bindings).
The game's controls:
Player 1
- Rotate sprite and set angle of movement: LEFT arrow
- Rotate sprite and set angle of movement: RIGHT arrow
- Move forward: UP arrow
- Shoot missile: ENTER key
Player 2
- Rotate sprite and set angle of movement: 'A' key
- Rotate sprite and set angle of movement: 'D' key
- Move forward: 'W' key
- Shoot missile: 'T' key
Relevant code:
The Key Bindings part:
// An action for every key-press. // Each action sets a flag indicating the key is pressed. leftAction = new AbstractAction(){ public void actionPerformed(ActionEvent e){ keysPressed1[0] = true; } }; rightAction = new AbstractAction(){ public void actionPerformed(ActionEvent e){ keysPressed1[1] = true; } }; // And so on... // .... // An action for every key-release. // Each action sets a flag indicating the key was released. // This is only necessary for some of the keys. leftReleased = new AbstractAction(){ public void actionPerformed(ActionEvent e){ keysPressed1[0] = false; } }; rightReleased = new AbstractAction(){ public void actionPerformed(ActionEvent e){ keysPressed1[1] = false; } }; // And so on... // .... // Binding the keys to the actions. inputMap.put(KeyStroke.getKeyStroke("UP"),"upAction"); inputMap.put(KeyStroke.getKeyStroke("LEFT"),"leftAction"); // etc... actionMap.put("upAction",upAction); actionMap.put("leftAction",leftAction); // etc...
In the Board class. It has most of the game's code.
This part checks the flags and reacts to key presses and releases.
keysPressed1 = tank1.getKeys(); // get flags-array of tank1. keysPressed2 = tank2.getKeys(); // get flags-array of tank2. if(keysPressed1[0]==true) // if LEFT is pressed. tank1.setAngle(tank1.getAngle()-3); if(keysPressed1[1]==true) // if RIGHT is pressed. tank1.setAngle(tank1.getAngle()+3); if(keysPressed1[2]==true){ // if UP is pressed. tank1.setDX(2 * Math.cos(Math.toRadians(tank1.getAngle()))); tank1.setDY(2 * Math.sin(Math.toRadians(tank1.getAngle()))); } if(keysPressed1[2]==false){ // if UP is released. tank1.setDX(0); tank1.setDY(0); } // And the same for the second player's keys...
This is mostly how reacting to key-presses and key-releases works in my program. When a key is pressed or released, a flag is set. The Board class reades the flags every game-loop cycle and reacts accordingly.
As I said, the program doesn't react correctly to more than 3 keys held at a time, probably because of the keyboard. Is there a way to code a solution?
Help will be very much appreciated.
Thanks a lot