Parallel Keyboard Inputs

Hi there,

I’m working on my first Java game at the moment (which is a sidescroller like R-Type) and I have a little problem:

I added a KeyAdapter to my JFrame in order to move the spaceship on the screen.
Actually that works fine but with this approach I can only do one thing at the same time. So I can only move the ship OR shoot but i can’t move the ship AND shoot at the same time.
I thought about parallelizing the input with threads somehow but I really have no clue how to do this.
Any ideas?

Cheers
Wolfner

P.S.:
This is how my KeyAdapter looks at the moment:

private KeyAdapter keyAdapter1 = new KeyAdapter() {
		public void keyPressed(java.awt.event.KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				model.playerPositionChanged(1);
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				model.playerPositionChanged(2);
			}else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				model.playerPositionChanged(3);
			}else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				model.playerPositionChanged(4);
			}else if(e.getKeyCode() == KeyEvent.VK_SPACE){
				model.shot();
			}
		};
		public void keyReleased(java.awt.event.KeyEvent e) {
			model.playerPositionChanged(0);
		}
	};

A standard approach is to store all the key strokes in a boolean array using the integer returned from e.getKeyCode() as the index. A quick search of these forums came up with this post http://www.java-gaming.org/index.php/topic,18715.0.html but I’m sure there is others.

EDIT*
Then in your update method of your ship you can check for each key you care about and handle it.

Oh, much simpler than i thought.
Thanks a lot!

Hi again,

I implemented the controls and it worked out so far but I have one problem:
I can go in all 8 directions while firing (3-button combination) but one!
If I go “Up-left” the application just bleeps once and the ship doesn’t move anywhere.

Cheers
Wolfner

Yes, the boolean array is ok, but one thing you have to watch out for might be presses that are to quick to catch.
During my mobile dev days I did a similar approach and had this problem.
The problems happens when you store the key presses/releases and then in the main loop, poll the array. By the next iteration, they key once pressed might have been unpressed.

The alternative is to store presses and releases in different arrays. Then after each processing in the main loop, untoggle the pressed by checking if it had been released.
i.e.


 if ( keyreleased[i] ) keypressed[i] = keyreleased[i] false;

This way you can even catch those quick presses.

Also, and this is why it was a problem with mobiles, some phones did not support repeats and some would not register a release if you were pressing two keys at once.

(posted this out of memory, I hope it is not failing me!)

Thanks a lot!
Just one further question:
Is there any way to override the delay between “KeyTyped” and “KeyPressed”?
(For example if you want to write “BBBBBBBBBBBBBBBBB”, then there is a delay between the first B and the remaining Bs)

That delay is OS dependent (and configurable) but you shouldn’t really be worrying about that.

Simply give your weapon a timestamp of when it was last fired, and give that weapon an interval between shots. Each gametick you check the weapon.canFire(), checking the whether currentTime > lastShotTime + weaponInterval, and if it returns true, call weapon.fire().

Timestamps might make the game act wierd, rather use a counter :wink:

True, but only in a fixed rate engine update.