Using InputListener
attached to images for touchscreen control I initially had the problem of the player being able to continuously jump and fix themselves to the top of the screen by simply holding the jump button.
This was resolved by adding a gdx.input.justTouched()
check before calling the jump()
method. However, this annoyingly then introduced a new issue of not being able to jump whilst holding a direction button. The direction button first has to be released before the jump will function.
if (player.presentState != Player.State.DEAD && player.presentState != Player.State.WIN) {
if (controller.isRightPressed() && player.body.getLinearVelocity().x <= 1.75)
player.body.applyLinearImpulse(new Vector2(0.1f, 0), player.body.getWorldCenter(), true);
if (controller.isLeftPressed() && player.body.getLinearVelocity().x >= -1.75)
player.body.applyLinearImpulse(new Vector2(-0.1f, 0), player.body.getWorldCenter(), true);
if (controller.isUpPressed() && controller.isJumpPressed() && player.presentState != Player.State.FALLING)
if (Gdx.input.justTouched()){
player.superJump();
}
if (controller.isDownPressed()){
player.dive();
}
if (controller.isJumpPressed() && player.presentState != Player.State.JUMPING){
if (Gdx.input.justTouched()) {
player.jump();
}
The jump()
method operates under two different state conditions.
If the player is state.ground
or state.running
, a positive y-impulse is applied.
If the player is state.falling
a slight positive y-impulse is applied with linearDampening
allowing the player to control the descent as to float/glide down.
This all works perfectly via my keyboard input implementation but not in my touchscreen one.
How to resolve this?