Reputation: 969
I am working on controls in libgdx. I have 3 buttons: up, right and left. Things are working fine if I click the buttons separately.
On clicking 2 buttons simultaneously, e.g. the right and up buttons, the player isn't having a smooth jump . It gets struck and is not rendering properly .
Part of my code:
//Up Button
buttonup.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
gamehero.heroBody.setLinearVelocity(0, 4f);
return true;
}
});
//Right Button
buttonright.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
gamehero.heroBody.setLinearVelocity(2f, 0);
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
gamehero.heroBody.setLinearVelocity(0, 0);
}
});
//Left Button
buttonleft.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if gamehero.heroBody.setLinearVelocity(-2f, 0);
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
gamehero.heroBody.setLinearVelocity(0, 0);
}
});
I have gone through many posts especially this but it didn't help me. I feel like something has to be done with pointers but I don't know how to use it with buttons. Is there any way to make the player have a smooth rendering if I touch 2 buttons at the same time?
Upvotes: 2
Views: 205
Reputation: 2163
Each button should only affect motion in one of your coordinate directions: x for Left/Right, y for Up. So rather than setting the velocity in the other direction to 0, you should leave it unchanged (i.e. set it to its current value which is presumably stored in gamehero
).
e.g. your Up button should set something like:
gamehero.heroBody.setLinearVelocity(gamehero.velocity.x, 4f);
Upvotes: 1