Reputation: 43
So, i am creating a joystic for a android game, so in my Touching class(implementing InputProcessor) it looks like a :
public class Touching implements InputProcessor{
int y = 0;
int x = 0;
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
x = Gdx.input.getX();
y = Gdx.input.getY();
if (x > 122 && x < 202 && y > 620 && y < 700 )
{
Core.player1.anmov(2); //button 1
}
if ( x > 122 && x < 202 && y > 520 && y < 600)
{
Core.player1.anmov(1); //button 2
}
if (x > 20 && x < 100 && y > 620 && y < 700)
{
Core.player1.anmov(3); //button 3
}
if (x > 222 && x < 302 && y > 620 && y < 700)
{
Core.player1.anmov(4); // button 4
}
return true;
}
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
x = Gdx.input.getX();
y = Gdx.input.getY();
if (x > 122 && x < 202 && y > 620 && y < 700)
{
Core.player1.anmov(7); // button 1
}
if ( x > 122 && x < 202 && y > 520 && y < 600)
{
Core.player1.anmov(8); // button 2
}
if (x > 20 && x < 100 && y > 620 && y < 700)
{
Core.player1.anmov(5); // button 3
}
if (x > 222 && x < 302 && y > 620 && y < 700)
{
Core.player1.anmov(6); // button 4
}
return false;
}
public boolean touchDragged(int screenX, int screenY, int pointer) {
x = Gdx.input.getX();
y = Gdx.input.getY();
return true;
}
so now, if i touch the button which represents moving on X, dragg to button which represents moving on Y, it is still moving on X, until i get my finger out of screen(the touchUP is calling), and then, it it standing, it does not move on Y..
Can anybody help me please? I think this is a very primitive, but i cant find a solution.
Upvotes: 1
Views: 185
Reputation: 8584
I would probably go for something like this in the player class update method itself
//Declare velocity on top so you just have to change this to fine tune speed.
float velocity = 10;
if (Gdx.input.isTouched()) //isTouched should register as long as a screen is touched, you can use pointers for multiple fingers.
{
if (touch within button) //just use Gdx.input.getX / Gdx.input.getY
{
position.X += velocity * Gdx.graphics.getDeltaTime();
{
//do for each button
}
Never used dragging for onscreen buttons myself but theoretically this should work since isTouched
registers as long screen is touched. Gdx.input.getX/Y
should update. You probably had something in there that only ran once and continued moving your player until a release was registered.
Upvotes: 1