# LibGdx: How do I combine 2 GestureListener objects from 2 seperate classes into 1 GestureDetector

I'm trying to make 2 separate classes, 1 a VirtualJoystick class and 1 a VirtualButton class. I want to implement a GestureListener class in each, it just seems like good code to do so, but when I go to my main program, how do I add both GestureListener classes?

Here's some pseudocode that I'm thinking of:

VirtualJoystick vjs = new VirtualJoystick(x, y, width, height, bgImage, fgImage);
VirtualButton vb    = new VirtualButton(x,  y,  width, height, upImage, dnImage);

GestureDetector joyGestureDetector = new GestureDetector( vjs.getDetector() );
GestureDetector btnGestureDetector = new GestureDetector(  vb.getDetector() );

// Here's what I don't know how to do
GestureDetector combinedGestureDetector = ?

Gdx.input.setInputProcessor(combinedGestureDetector);


but as you can see, I'm stuck on how to combine the two, if there's a better way let me know, I'm just trying to get my code to "think through well" and be atomically contained in classes.

• I have a feeling I'm going about this all wrong, I know now that I can use Gdx.input.isTouched(); but I'm not sure how to do multiple buttons and a joystick just yet. – jaysonpowers Apr 22 '13 at 23:00

The quick and dirty way to fix this is to use InputMultiplexer. This allows you to pass those gesture events to both detectors.

VirtualJoystick vjs = new VirtualJoystick(x, y, width, height, bgImage, fgImage);
VirtualButton vb    = new VirtualButton(x,  y,  width, height, upImage, dnImage);

GestureDetector joyGestureDetector = new GestureDetector( vjs.getDetector() );
GestureDetector btnGestureDetector = new GestureDetector(  vb.getDetector() );
//Note: you need return false in your event handlers like touchDown(...),
//so that the event gets passed to both detectors you added to the multiplexer.
InputMultiplexer multiplexer= new InputMultiplexer(joyGestureDetector,btnGestureDetector);

Gdx.input.setInputProcessor(multiplexer);