2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ – jaysonpowers Apr 22 '13 at 23:00
1
\$\begingroup\$

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);
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.