简体   繁体   中英

Input events not working simultaneously on android devices in libgdx

I am making a simple platform game in Libgdx... in which I have made the player to move left, move right and jump. The code works fine on Desktop but on Android devices, Jump is not fired when the player moves left or right. It looks strange. Here is my code...

private void updatePlayerForUserInput(float deltaTime) {

    // check input and apply to velocity & state
    if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.87f, 1,0,1f)) && world.player.grounded)
    {
        world.player.velocity.y += world.player.JUMP_VELOCITY;
        world.player.state =2;
        world.player.grounded = false;
    }

    if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.1f,0,1f))
    {
        world.player.velocity.x -=world.player.MAX_VELOCITY;
        if (world.player.grounded)
            world.player.state =1;
        world.player.facesRight = false;
    }

    if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.2f, 0.3f,0,1f))
    {
        world.player.velocity.x =world.player.MAX_VELOCITY;
        if (world.player.grounded)
            world.player.state =1;
        world.player.facesRight = true;

    }
}

private boolean isTouched(float startX, float endX , float startY, float endY)
{
    // check if any finge is touch the area between startX and endX
    // startX/endX are given between 0 (left edge of the screen) and 1 (right edge of the screen)
    for (int i = 0; i < 2; i++)
    {
        float x = Gdx.input.getX() / (float) Gdx.graphics.getWidth();
        float y = Gdx.input.getY() / (float) Gdx.graphics.getHeight();
        if (Gdx.input.isTouched(i) && (x >= startX && x <= endX) && (y>=startY && y<= endY))
        {
            return true;
        }
    }
    return false;
}

I took the idea from the demo platform game SuperKoalio by mzencher at

https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/superkoalio/SuperKoalio.java

Please suggest

This code:

    float x = Gdx.input.getX() / (float) Gdx.graphics.getWidth();
    float y = Gdx.input.getY() / (float) Gdx.graphics.getHeight();

Is always getting the x/y from the first active touch. You need to check the "i'th" active touch. Like this:

for (int i = 0; i < 20; i++) {
    if (Gdx.input.isTouched(i)) {
      float x = Gdx.input.getX(i) / (float) Gdx.graphics.getWidth();
      float y = Gdx.input.getY(i) / (float) Gdx.graphics.getHeight();
      if ((x >= startX && x <= endX) && (y>=startY && y<= endY)) {
          return true;
      }
}
return false;

Also, you should probably iterate through all 20 possible touch points, since up to 20 touch points can be tracked by the hardware. (Try putting three fingers in the "jump" region and then add a fourth finger in the "move left" region.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM