Mr. Developer
Mr. Developer

Reputation: 3417

detect when a finger leaves the screen and update pointers

I'm making a game where I accelerate holding two fingers on the screen , a finger in the left half and a finger in the right half of the display . if you release your finger and let down the other , whatever it is I have to bend the vehicle based on the location of the finger . If you are located in the right half ( Gdx.grapchis.getWidth / 2 ) then I bend to the right ... and so the left .

part of input processor:

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            if(pointer < 2)
            {
                CoordinateTouch tmp = new CoordinateTouch();
                tmp.x = screenX;
                tmp.y = screenY;
                coordinates.add(tmp);
            }
            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            coordinates.clear();
            return false;
        }

my array of coordinates:

public class CoordinateTouch{
    float x;
    float y;
}

List<CoordinateTouch> coordinates;

control pointer in render method (group is my texture):

if(coordinates.size() > 1)
    {
        group.addAction(parallel(moveTo(realDest.x, realDest.y, (float) 15)));           
    }
    else
    {
        group.addAction(delay((float)1.5));
        group.clearActions();
        if(Gdx.input.isButtonPressed(0)) {
            if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
                group.addAction(parallel(rotateBy(velocityRotazionShip, (float) 0.03)));
            } else {
                group.addAction(parallel(rotateBy(-velocityRotazionShip, (float) 0.03)));
            }
        }
    }

My problem is that if I leave one finger detects it , and so far so good , but if I just leaned back his finger pulled away, I do not update the pointers and did not produce the piece of code group.addAction .

I also tried isButtonPressed and isKeypressed, isTouched(index) , but the result is the same.

Sorry for the English and I hope to have been clear .

Upvotes: 0

Views: 225

Answers (1)

Robert P
Robert P

Reputation: 9793

If i understood you corectly you have 3 cases:

  1. Both disply sides are pressed -> move
  2. Left display side pressed -> lean left
  3. Right display side pressed -> lean right

If that assumption is correct, you would only need to booleans:

boolean touchLeft, touchRight

Inside the touchDown you could do something like that:

 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
 if (screenX < Gdx.graphics.getWidth()/2)
     touchLeft = true;
 else
     touchRight = true;
 }

And in the touchUp:

public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    if (screenX < Gdx.graphics.getWidth()/2)
        touchLeft = false;
    else
        touchRight = false;
}

Now inside the render you can just say:

if (touchLeft && touchRight)
    // move
else if (touchLeft)
    // lean left
else if (touchRight)
    // leanRight
else
    // do nothing or something else

If you want to support multiple fingers/side, you can change the booleans to ints, giving the number of fingers/side. In the touchDown increment it, in the touchUp decrement it and in the render ask, if it is > 0.

Upvotes: 1

Related Questions