I'm making a racing game with LibGDX and Box2D. I've been using this tutorial to implement the basic physics, and it works properly.
My main problem is to implement two different sets of keys (UP-DOWN-LEFT-RIGHT and ASDW) to control two different instances of a Car class on a same screen.
The code I have yet for handling one set of input (for KeyDown):
@Override
public boolean keyDown(int keycode) {
if (keycode == Input.Keys.UP) {
if (!pressedKeys.contains(Key.Up)) {
pressedKeys.add(Key.Up);
}
} else if (keycode == Input.Keys.DOWN) {
if (!pressedKeys.contains(Key.Down)) {
pressedKeys.add(Key.Down);
}
} else if (keycode == Input.Keys.LEFT) {
if (!pressedKeys.contains(Key.Left)) {
pressedKeys.add(Key.Left);
}
} else if (keycode == Input.Keys.RIGHT) {
if (!pressedKeys.contains(Key.Right)) {
pressedKeys.add(Key.Right);
}
}
return false;
}
(KeyUp):
@Override
public boolean keyUp(int keycode) {
if (keycode == Input.Keys.UP) {
if (pressedKeys.contains(Key.Up)) {
pressedKeys.remove(Key.Up);
}
} else if (keycode == Input.Keys.DOWN) {
if (pressedKeys.contains(Key.Down)) {
pressedKeys.remove(Key.Down);
}
} else if (keycode == Input.Keys.LEFT) {
if (pressedKeys.contains(Key.Left)) {
pressedKeys.remove(Key.Left);
}
} else if (keycode == Input.Keys.RIGHT) {
if (pressedKeys.contains(Key.Right)) {
pressedKeys.remove(Key.Right);
}
}
return false;
}
How should I do to be able to handle UP-DOWN-LEFT-RIGHT and ASDW keys simultaneously?
Note: A Car is instantiated in the main class.
I can always add more code if needed.