0
\$\begingroup\$

I got a working pong game in LibGDX but when I touch the screen the player x automaticly jumps to the coordinates I touched. I wan't my game to detect I touched and then move the player to the left/right if I swipe my finger to the left or the right.

This is what I tried so far:

if(Gdx.input.isTouched() && Gdx.input.getY()>240*screenheight){
  if(tapped)TouchX = Gdx.input.getX();
    tapped = false;
    DragX = Gdx.input.getX();

    distance = player.getX() - TouchX;

    PlayerX = Gdx.input.getX() + distance;


    Gdx.app.log("PlayerX "+player.getX(), "Distance "+distance);
    Gdx.app.log("DragX ", ""+DragX);
}else{
    tapped = true;
}

Here I update the player X coordinates

player.setX(PlayerX);

Does anyone know how to make this work?

\$\endgroup\$
1
\$\begingroup\$

You can use InputAdapter class and methods

touchDown(int screenX, int screenY, int pointer, int button)
Called when the screen was touched or a mouse button was pressed.

boolean touchDragged(int screenX, int screenY, int pointer)
Called when a finger or the mouse was dragged.

boolean touchUp(int screenX, int screenY, int pointer, int button)
Called when a finger was lifted or a mouse button was released.
\$\endgroup\$
1
\$\begingroup\$

You could update the position over time:

currentPos = currentPos + Math.signum(currentPos - touchedPos) * wayPerTime * deltaTime;
  • wayPerTimestep is your velocity.
  • deltaTime is the time passed since the last update.
  • currentPos is the current position of your player (might be x, y or both as vector).
  • touchedPos is the position the player has clicked on the screen (might be x, y or both as vector).

Math.signum() gives you +1 if the value is positive or -1 if negative. Otherwise 0. This takes care about the same speed regardless how big the distance is between player and clicked position.

The code uses this formula for calculating the way s:

Way is speed times time

\$\endgroup\$
  • \$\begingroup\$ I can't figure out your solution It ends up with making the player x going down each gameloop by 1 I updated my question with what I tried so far can you maybe take a look at that? \$\endgroup\$ – Stefan Jan 28 '15 at 13:25
  • \$\begingroup\$ This is because you don't incorporate the time and velocity. You implemented only the half of the equation. Use Math.signum() to figure out the direction to move. Then multiply it with the unsigned way you want to move to correct the position. If currentpos - touchedPos is 0 the result of Math.signum is 0 as well and so is the absolute way you add to the current position: The player doesn't move any longer. \$\endgroup\$ – Sebastian Barth Jan 28 '15 at 13:32
  • \$\begingroup\$ Allright how do I exactly get the velocity? I'm not using dx for the player for its speed just the coordinates the mouse is currently at. \$\endgroup\$ – Stefan Jan 28 '15 at 13:38
  • \$\begingroup\$ You have to design it by yourself. For example you could say the player moves within one second for 50px. If you have the delta time (in seconds) passed since your last update you can simply multiply it with that value: x += Gdx.graphics.getDeltaTimer() * velocity \$\endgroup\$ – Sebastian Barth Jan 28 '15 at 13:51
  • \$\begingroup\$ Did it help? Anything you don't understand yet? \$\endgroup\$ – Sebastian Barth Feb 2 '15 at 9:31

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.