2015-12-12 20 views
1

我的一些朋友正在合作開發一款適用於我們高中計算機科學年終任務的應用程序。我們正在製作一款改裝的乒乓球遊戲。我正在開發Android版本,兩個正在開發iOS版本。到目前爲止,一切進展順利,我們在課堂上使用的主要語言是Python,因此轉換到Java已經有點像學習曲線了。Android多點觸控幫助 - 同時拖動兩個對象

我一直在努力拖動兩個槳。我已經在這裏和Android開發者頁面上閱讀了許多帖子,大部分代碼都來自那裏,但我無法完全理解。當一個指針在設備上時,兩個手柄都可以很好地拖動,但一旦第二個指針觸擊屏幕,它就會忽略第一個指針,並且只使用第二個指針,直到它被釋放。這是我的代碼...

private final static int INVALID_POINTER_ID = -1; 
    private int mActivePointerId = INVALID_POINTER_ID; 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     int action = MotionEventCompat.getActionMasked(ev); 

     switch (action) { 

      case MotionEvent.ACTION_DOWN: { 
       final int pointerIndex = MotionEventCompat.getActionIndex(ev); 
       mLastTouchX1 = MotionEventCompat.getX(ev, pointerIndex); 
       mActivePointerId = MotionEventCompat.getPointerId(ev, 0); 
       break; 
      } 

      case MotionEvent.ACTION_POINTER_DOWN:{ 
       final int pointerIndex = MotionEventCompat.getActionIndex(ev); 
       if (pointerIndex == 1) { 
        mLastTouchX1 = MotionEventCompat.getX(ev, pointerIndex); 
       } 
       mActivePointerId = MotionEventCompat.getPointerId(ev, pointerIndex); 
      } 


      case MotionEvent.ACTION_MOVE: { 
       final int pointerIndex = 
         MotionEventCompat.findPointerIndex(ev, mActivePointerId); 


        final float x = MotionEventCompat.getX(ev, pointerIndex); 
        final float y = MotionEventCompat.getY(ev, pointerIndex); 

        final float deltaX = x - mLastTouchX1; 
        paddle1.update(deltaX, screenX, y, screenY); 
        paddle2.update(deltaX, screenX, y, screenY); 


        mLastTouchX1 = x; 

        invalidate(); 
        break; 
       } 

      case MotionEvent.ACTION_UP: { 
       break; 
      } 
      case MotionEvent.ACTION_CANCEL: { 
       break; 
      } 

      case MotionEvent.ACTION_POINTER_UP:{ 
       final int pointerIndex = MotionEventCompat.getActionIndex(ev); 
       final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); 

       if (pointerId == mActivePointerId) { 
        // This was our active pointer going up. Choose a new 
        // active pointer and adjust accordingly. 
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0; 
        mLastTouchX2 = MotionEventCompat.getX(ev, newPointerIndex); 
        mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); 
       } 
       break; 
      } 
     } 

Paddle1和paddle2是我的槳類的實例。這是我在課堂上的更新方法。

public void update (float x, int screenX, float y, int screenY){ 
    if (y > screenY/2){ 
     rect1.left = rect1.left + x; 
     rect1.right = rect1.left + length; 
     if (rect1.left < 0){ 
      rect1.left=0; 
      rect1.right = rect1.left + length; 
     } 
     if (rect1.right > screenX){ 
      rect1.left = screenX - length; 
      rect1.right = screenX; 
     } 
    } 
    else{ 
     rect2.left = rect2.left + x; 
     rect2.right = rect2.left + length; 
     if (rect2.left < 0){ 
      rect2.left=0; 
      rect2.right = rect2.left + length; 
     } 
     if (rect2.right > screenX){ 
      rect2.left = screenX - length; 
      rect2.right = screenX; 
     } 
    } 

我意識到我的代碼可能是草率的,但我的最終目標是能夠同時移動兩個槳。如果任何人都可以向我展示我需要添加或調整的內容,那將會很棒。

謝謝。

編輯:使用Android Studio。

回答

1

在網上搜索,發現了好文章您的問題, vogella.com/tutorials/AndroidTouch/article.html 接下來的時間但是做一個快速的搜索對谷歌之前你問Questions.Rember互聯網是一個大的地方充滿信息;)