0

私は Libgdx で単純なプラットフォーム ゲームを作成しています... プレイヤーが左に移動し、右に移動してジャンプするようにしました。コードはデスクトップでは問題なく動作しますが、Android デバイスでは、プレーヤーが左右に移動しても Jump は発生しません。奇妙に見えます。これが私のコードです...

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;
}

mzencher のデモ プラットフォーム ゲーム SuperKoalio から着想を得ました。

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

提案してください

4

1 に答える 1

1

このコード:

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

最初のアクティブなタッチから常に x/y を取得しています。「i番目」のアクティブなタッチを確認する必要があります。このような:

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;

また、ハードウェアは最大 20 のタッチ ポイントを追跡できるため、考えられる 20 のタッチ ポイントすべてを反復処理する必要があります。(「ジャンプ」領域に 3 本の指を置き、「左へ移動」領域に 4 本目の指を追加してみてください。)

于 2013-06-17T17:46:20.940 に答える