1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ I really don't understand where the problem is. Can you please elaborate where you have problems to route key input events to the correct entity? \$\endgroup\$
    – Philipp
    Mar 29, 2016 at 19:09
  • \$\begingroup\$ Are both players controlling the same car? \$\endgroup\$
    – jgallant
    Mar 29, 2016 at 19:34
  • \$\begingroup\$ each player is controlling ONE car \$\endgroup\$
    – Rasoplast
    Mar 31, 2016 at 1:19

1 Answer 1

1
\$\begingroup\$

It's a little unclear what you are trying to accomplish with your pressed keys object, but here's a suggestion.

Create a car controller class which takes a car as a parameter in its constructor. And has several action objects that you can bind to. Here's an example for the accelerator (I haven't compiled this so it may need some tweaking)

public class CarController{
    public ICallable<Void> pushAccelerator;
    public ICallable<Void> releaseAccelerator;
    public CarController(final Car car)
    {
       pushAccelerator = new ICallable<Void>(){
           public Void call(){
               car.pushAccelerator();
               return null;
           }
       }
       releaseAccelerator = new ICallable<Void>(){
           public Void call(){
               car.releaseAccelerator();
               return null;
           }
       }
    }
}

Then in your input handler you could have two mappings, one when the key is pressed, one when the key is released.

private Map<Integer,ICallable<Void>> keyPressedHandlers = new HashMap<Integer,ICallable<Void>>();
private Map<Integer,ICallable<Void>> keyUpHandlers = new HashMap<Integer,ICallable<Void>>();

public void mapInputs(Car car1, Car car2){
   CarController controller1 = new CarController(car1);
   CarController controller2 = new CarController(car2);
   keyPressedHandlers.put(Keys.W,controller1.pushAccelerator);
   keyPressedHandlers.put(Keys.UP,controller2.pushAccelerator);
   keyUpHandlers.put(Keys.W,controller1.releaseAccelerator);
   keyUpHandlers.put(Keys.UP,controller2.releaseAccelerator);
}

public boolean keyDown(int keycode) {
    if(keyPressedHandlers.containsKey(keycode)){
        keyPressedHandlers.get(keycode).call();
    }
 }

 public boolean keyUp(int keycode) {
    if(keyUpHandlers.containsKey(keycode)){
        keyUpHandlers.get(keycode).call();
    }
 }

This sort of mapping approach even makes it so you could eventually allow a user to map their own keys to the car's actions.

\$\endgroup\$
1
  • \$\begingroup\$ Sorry for being unclear. Basically, I just want to implement 2 players mode for my racing game, and make the game listen to multiple keys simultaneously :) I don't know if that was more clear... \$\endgroup\$
    – Rasoplast
    Mar 31, 2016 at 1:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .