Java – How to implement different events with single and double click in libgdx?

How to implement different events with single and double click in libgdx?… here is a solution to the problem.

How to implement different events with single and double click in libgdx?

I’m developing Android games using LibGdx. I’ve implemented the gesture listener class in my InputHandler class. Now in the click method, I have to implement two functions when clicking and double-clicking, namely short jump and long jump, respectively. When I try to implement it using the count value of the function, the problem is that when I double click on the screen, the count value first becomes 1 and then 2, so it does not go into the second if statement and is characterized by a short jump. So how do you tell the difference between single hop and double hop? Here’s the code

@Override
public boolean tap(float x, float y, int count, int button) {
     TODO Auto-generated method stub

if(count==1)
    {
        feature 1
    }
    if(count==2)
    {
        feature 2

}

return true;
    }

Solution

Two solutions came to mind:

  1. Using latency techniques, this will be accompanied by the following steps:

    • When tap-count is 2: Double-click to trigger Action
    • When tap-count is 1: WAITING for a period of time, if there is no second click, the click Action is triggered

The code should look like this:

if(count==1)
{
    if(wait) //wait is a boolean tells if it was tapped once already
    {
        if(getCurrentTime() - startTime > interval) //getCurrentTime() return current time or frame count or something
        {
            wait = false;
            feature 1
        }
    }
    else
    {
        wait = true;
        startTime = getCurrentTime(); start time keeps the time when tapped first one
    }
}
if(count==2)
{
    feature 2

}

return true;
}   

The question I see here starts with “How do I choose a long enough time interval to wait for a second click?” – If you select short, you won’t be able to double-click, if it’s too long, there’s a delay and the user sees that the character doesn’t jump directly after clicking

  1. Jump in chunks and trigger them based on the number of clicks

And it depends on your “jump” mechanic. If it is like this:

One. Afterburner jump

B。 Keep adding some force for a while to keep the object in the air

C。 Avoid applying force to lower the object back to the ground

You can modify the time to keep the object in the air longer. If your “jump mechanism” is more like pulse afterburner (in the example above, it would be a variant without a B point), you can add the force to A again for a while, and if the user clicks a second time before this interval ends, continue adding it, just longer.

This solution eliminates the lag problem, but it depends on the “hopping mechanism” you choose.

greeting
Michael

Related Problems and Solutions