2016-05-26 9 views
1

マルチタッチの実装に問題があります。しばらく検索しました。現在の実装は機能しますが、他の入力は中断します。このコードは、入力領域を処理/描画する入力コントローラの一部です。モーションイベントはOnTouchEvent()からこの関数を介して渡されます。 正しく追跡するためにポインタIDを使用する必要がありますが、ポインタのインデックスが範囲外であることを実装しようとするたびに読んでいます。 私は本当にOKマルチタッチイベントを適切に処理するandroid

public void handleInput(MotionEvent motionEvent,LevelManager l, SoundManager sound, Viewport vp){ 
    int pointerCount = motionEvent.getPointerCount(); 
    for (int i = 0; i < pointerCount; i++) { 
     int x = (int) motionEvent.getX(i); 
     int y = (int) motionEvent.getY(i); 
     if(l.isPlaying()) { 

      switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 

       case MotionEvent.ACTION_DOWN: 
        if (right.contains(x, y)) { 
         l.player.setPressingRight(true); 
         l.player.setPressingLeft(false); 
        } else if (left.contains(x, y)) { 
         l.player.setPressingLeft(true); 
         l.player.setPressingRight(false); 
        } else if (jump.contains(x, y)) { 
         l.player.startJump(sound); 
        } else if (shoot.contains(x, y)) { 
         if(l.player.pullTrigger()){ 
          sound.playSound("shoot"); 
         } 
        } else if (pause.contains(x, y)) { 
         l.switchPlayingStatus(); 

        } 

        break; 


       case MotionEvent.ACTION_UP: 
        if (right.contains(x, y)) { 
         l.player.setPressingRight(false); 
        } else if (left.contains(x, y)) { 
         l.player.setPressingLeft(false); 
        } 


        break; 


       case MotionEvent.ACTION_POINTER_DOWN: 
        if (right.contains(x, y)) { 
         l.player.setPressingRight(true); 
         l.player.setPressingLeft(false); 
        } else if (left.contains(x, y)) { 
         l.player.setPressingLeft(true); 
         l.player.setPressingRight(false); 
        } else if (jump.contains(x, y)) { 
         l.player.startJump(sound); 
        } else if (shoot.contains(x, y)) { 
         if(l.player.pullTrigger()){ 
          sound.playSound("shoot"); 
         } 
        } else if (pause.contains(x, y)) { 
         l.switchPlayingStatus(); 
        } 
        break; 


       case MotionEvent.ACTION_POINTER_UP: 
        if (right.contains(x, y)) { 
         l.player.setPressingRight(false); 
        } else if (left.contains(x, y)) { 
         l.player.setPressingLeft(false); 
        } else if (shoot.contains(x, y)) { 
         //Handle shooting here 
        } else if (jump.contains(x, y)) { 
         //Handle more jumping stuff here later 
        } 
        break; 
      } 
     }else {// Not playing 
      //Move the viewport around to explore the map 
      switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 
       case MotionEvent.ACTION_DOWN: 
        if (right.contains(x, y)) { 
         vp.moveViewportRight(l.mapWidth); 
        } else if (left.contains(x, y)) { 
         vp.moveViewportLeft(); 
        } else if (jump.contains(x, y)) { 
         vp.moveViewportUp(); 
        } else if (shoot.contains(x, y)) { 
         vp.moveViewportDown(l.mapHeight); 
        } else if (pause.contains(x, y)) { 
         l.switchPlayingStatus(); 
        } 
        break; 
      } 



     } 
    } 

} 

答えて

0

いくつかの助けを使用することができます私はインデックスを追加し、MotionEventCompatインタフェースを介してイベントに渡すことによって、目的の動作を達成することができましたMotionEventCompatを使用して、答えを見つけました。

 int index = MotionEventCompat.getActionIndex(motionEvent); 
     int x = (int) MotionEventCompat.getX(motionEvent, index); 
     int y = (int) MotionEventCompat.getY(motionEvent, index); 

の代わり int x = (int) motionEvent.getX(i); int y = (int) motionEvent.getY(i);

 for (int i = 0; i < pointerCount; i++) { 

た後、これは正直にそれが本当にハック感じているように私には、このような謎のように思える、私は両方のポインタを反復することは、今必要な場合にもわからないんだけど。私は自分自身は、forループ今冗長私が正しかった

+0

でさらに当惑する前に、私は試してみて、バック

//ループのために確かに冗長用で編集コメントをするつもりです、私は念のため持っていました。それは私には奇妙に感じるが、私はそれに行くだろう、 – naahn

関連する問題