1
\$\begingroup\$

In my Libgdx project i need to handle several types of inputs.

In particular these input events are needed in order to interact with the Actors that the user puts in the Stage.

The possible interactions are:

1) Drag And Drop with DragAndDrop class.

2) Rotate the Actor (90 degrees clockwise each time)

3) Remove the Actor

I thought that i could rotate the Actor with a simple click (touchDown), and remove the Actor with double tap, but this strategy gives rise to some issues.

1) When the user does double tap on the Actor, before the Actors gets removed it rotates 2 times.

2) If the user wants to drag and drop the Actor, when he clicks on the Actor it rotates.

Then, i thought that i could rotate the Actor in the touchUp method, only if the current position of the Actor is the same as before in order to avoid to rotate the Actor when the user just want to move it.

This gives rise to another problem. In order to detect the double tap, i need to implement GestureListener interface that does not have the touchUp method!

I could remove the actor when the user does long press on it, but this will be a game that will require fast interaction.

So.. i need your help, any kind of help, thank you very much!

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The base idea here is that you have to separate the concepts:

  • Input
  • Command
  • Behaviour

So basically, instead of linking the onTouchDown event to actor.rotate(90), you need an input manager that interprets the movements of the user:

  • click
  • doubleClick
  • drag

This input manager will generates these events from what you get from the base layers of your system. Some libraries can do that for you, but some don't and you have to manage it yourself; I don't know how libgdx does it.

But the thing is, if you get a click, you have to wait a bit to make sure that you don't get a double click, and when you get a double click you have to make sure you don't get two clicks. This is the main task of the input manager.

Then you need to translate these events into commands.

The command manager receives Input events, and translates them into "what do I do with that". So for instance, the command managers knows that in a specific context, the click is associated with a actor.rotate(90) action.

This allows you to

  • easily test your inputs in an isolated way
  • split your input from the behaviour of the game (no "is the user tapping, double-taping or dragging" in the game code (actors), only commands (you'll still need to handle dragging in a more complex way, like startDragging and stopDragging and moveWhileDragging, though ))
  • change the commands associated with the input easily
    • allow the user to customize the inputs
    • allow for a much easier change of context
\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your answer. The fact is that i must obey to the rules of Libgdx framework that has its own mechanism to handle the input. \$\endgroup\$
    – Loris
    Commented Oct 30, 2015 at 8:47

You must log in to answer this question.

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