1
\$\begingroup\$

So, I have these buttons where I have implemented a method to handle 'touch and hold' (without polling). If the player touches and holds the walk button ..it will keep walking till the button is released. Problem is when I combine jump with it ...
In the update method,

if (activeTouch) {
        translateScreenToWorldCoordinates(touchCoords.x, touchCoords.y);
         if (touchPoint.x < 5) {
           // Code for walk left
        } else if (touchPoint.x > VIEWPORT_WIDTH - 5) 
        {
            // Code for walk Right
        } else if ((touchPoint.x > 6 && touchPoint.x < VIEWPORT_WIDTH - 6) && player.isGrounded() && player.getState() != Player.State.Falling) { 
           // Code for Jump
        }
    }

The touchdown and touchup events,

 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        touchCoords.set(screenX, screenY);
        activeTouch = true;

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        activeTouch = false;
        return false;
    }

So i can move right,left and also jump. But my problem is, while walking if I jump ... it will jump and then stop walking. How to continue walking after the jump if the player is still holding onto the walk button?

\$\endgroup\$
  • 1
    \$\begingroup\$ This is a multi-touch problem I think, probably caused by the fact that you set "activeTouch=false;" on "touchUp". So if you hold ur finger down the entire time, then tap the jump-button to jump with another finger, after the jump "touchUp" will be called, setting activeTouch to false. Look for information on handling multiTouch and pointers, I don't know enough to give an answer \$\endgroup\$ – Green_qaue Jul 25 '15 at 16:31
2
\$\begingroup\$

You should register the pointer you are walking with. Pointers are the number of touches on the screen and they are provided by the listeners you are using.

I think if you change this it to something like below you can get it to work:

@Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if (int pointer != 0) //Guessing first is 0, might be 1.
        {
            activeTouch = false;
        }
        return false;
    }

Just log the pointers to see the output and set accordingly.

But why do you need the active touch? Coding the movement straight into the listeners is no problem. Or better to make two extra methods for walking and jumping. I cannot look into the rest of your code but something like this should work. The listeners only trigger when there are actual touches so do not bother setting up a boolean for that.

private void move(screen x, screen y)
{
    translateScreenToWorldCoordinates(touchCoords.x, touchCoords.y);
    if (touchPoint.x < 5) {
           // Code for walk left
        } else if (touchPoint.x > VIEWPORT_WIDTH - 5) 
        {
            // Code for walk Right
        }
}

private void jump(int screenX, int screenY)
{
    translateScreenToWorldCoordinates(touchCoords.x, touchCoords.y);
    if ((touchPoint.x > 6 && touchPoint.x < VIEWPORT_WIDTH - 6) && 
        player.isGrounded() && player.getState() != Player.State.Falling) 
    { 
           // Code for Jump
    }
}

Then just add both to the listener.

public boolean touchDown(int screenX, int screenY, int pointer, int button)        
{
        move(screenX, screenY);
        jump(screenX, screenY);    
        return false;
}
\$\endgroup\$
  • \$\begingroup\$ I am using the activeTouch boolean to implement a 'touch and hold' without using polling. If user touches the bool is true until a touchUp occurs and any appropriate action (like run) will be applied continuously while it is true (that is while user is still holding after a touch). I have tried your method, looks cleaner, but isn't working for a touch and hold scenario. It works but only as a single touch event.. how can I implement a touch and hold with it? Thanks. \$\endgroup\$ – redGREENblue Jul 26 '15 at 18:20
  • \$\begingroup\$ Oops, just realized that it just registers a touchdown and does not keep sending indeed. You could still do what's in my first code. Just registering the first touch/pointer which should probably always be the walk touch. Unless the player jumps and he is able to move when in the air. In which case you could add statements to check for that and when in the air and touch 1 is not released you register touch 2 as the walking touch. \$\endgroup\$ – Madmenyo Jul 26 '15 at 18:53
  • \$\begingroup\$ One would think a function like this should have been implemented into the InputProcessor by now. Or the next day it was written imho. If you know your way around you could extend GestureListener that is extended from InputAdapter and do it yourself too. \$\endgroup\$ – Madmenyo Jul 26 '15 at 19:00

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.