2016-08-21 7 views
1
for (byte i = 0; i < 20; i++) {maxDistance = 10 * Gdx.graphics.getDeltaTime(); 
     if (Gdx.input.isTouched(i) && Gdx.input.getY()<= 400) { 
      player1TouchPosition.set(Gdx.input.getX(i), Gdx.input.getY(i), 0); 
      camera.unproject(player1TouchPosition); 
     } 
     player1Tmp.set(player1TouchPosition.x, player1TouchPosition.y).sub(player1Rectangle.x, player1Rectangle.y); 
     if (player1Tmp.len() <= maxDistance) { 
      player1Rectangle.x = player1TouchPosition.x; 
      player1Rectangle.y = player1TouchPosition.y; 
     } else { 
      player1Tmp.nor().scl(maxDistance); 
      player1Rectangle.x += player1Tmp.x; 
      player1Rectangle.y += player1Tmp.y; 
     } 
     if (Gdx.input.isTouched(i) && Gdx.input.getY() >= 401) { 
      player2TouchPosition.set(Gdx.input.getX(i), Gdx.input.getY(i), 0); 
      camera.unproject(player2TouchPosition); 
     } 
     player2Tmp.set(player2TouchPosition.x, player2TouchPosition.y).sub(player2Rectangle.x, player2Rectangle.y); 
     if (player2Tmp.len() <= maxDistance) { 
      player2Rectangle.x = player2TouchPosition.x; 
      player2Rectangle.y = player2TouchPosition.y; 
     } else { 
      player2Tmp.nor().scl(maxDistance); 
      player2Rectangle.x += player2Tmp.x; 
      player2Rectangle.y += player2Tmp.y; 
     } 
    } 

こんにちは私はこのコードを使用してタッチ位置に移動します。しかし、私はマルチタッチが必要です。動いていない。 player2を追加すると、それは動作しません。私はマルチタッチがどのように理解できませんでした。どうすれば修正できますか?Java LibGDXマルチタッチの問題

答えて

1

なぜあなたはInputProcessorを使用しませんか? インタフェースあなたは最大2つのタッチを使用することができ、上記の例では

@Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
     if(pointer =< 2){ 
      touches.get(pointer).touchX = screenX; 
      touches.get(pointer).touchY = screenY; 
      touches.get(pointer).touched = true; 
     } 
     return true; 
    } 

から1つの方法の一例を示します。 実際に1ポインターは1タッチです。

Documentation

screenXscreenYタッチ位置です。あなたの正射投影カメラと比較してこの位置を拡大する必要があることに注意してください。ポインターはイベントのポインターです。

あなたが作成した場合InputProcessorあなたは

Gdx.input.setInputProcessor(/*Your class*/); 

EDITでそれを起動することができます。

例をコメントから:

for (Button button : /*ArrayList*/{ 
      if (positionX >= button1.getX() && positionX <= button1.getX() + button1.getWidth() && 
        positionY >= button1.getY() && positionY <= button1.getY() + button1.getHeight()){ 
     //Update the position from the specific button 
} 

をあなたがからメソッド内でこのコードを使用しますインタフェースtouchDragged()

+0

長方形が2つあり、長方形が1つ上、下が1つあります。したがって、まず、位置が上か下かをチェックします。私は 'touchDown'の代わりに' touchDragged'を使うのが好きです。 'touchDragged'はまた、' InputProcessor'インターフェースからのメソッドです。 –

+0

その後、長方形1と長方形2の変数を初期化します。renderメソッドでは、新しい位置を更新する必要があります。 –

+0

私はあなたを助けたと思う! :) –