2012-05-13 46 views
1

我正在爲基於android的surfaceview的遊戲設計虛擬按鈕。我已經重寫了onTouch(MotionEvent事件)方法來執行多點觸控功能,但它不像我想要的那樣工作。當觸摸一個手指時,第二個手指不會被識別,因此我無法按照需要執行功能。這是我重寫的方法的代碼。Android多點觸控無法正常工作

@Override 
public boolean onTouchEvent(MotionEvent event){ 

    touchX = (int)event.getX(); 
    touchY = (int)event.getY(); 

    int action = event.getActionMasked();//event.getAction() & MotionEvent.ACTION_MASK; 

    if(touchX >= 0 && touchX <= leftbutton.getWidth() && touchY >= this.getHeight() - 100 && touchY <= this.getHeight()){ 
     if (action == MotionEvent.ACTION_DOWN){ 
      starship.setLeftRight(1, 0, true); 
      leftButton.update(touchedleftbutton); 
     } 
     else if (action == MotionEvent.ACTION_UP){ 
      starship.setLeftRight(0, 0, false); 
      leftButton.update(leftbutton); 
     } 
     else if (action == MotionEvent.ACTION_POINTER_DOWN){ 
      starship.setLeftRight(1, 0, true); 
      leftButton.update(touchedleftbutton); 
     } 
     else if (action == MotionEvent.ACTION_POINTER_UP){ 

      starship.setLeftRight(0, 0, false); 
      leftButton.update(leftbutton); 
     } 
    } 
    else if (touchX >= 20 + leftbutton.getWidth() && touchX <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY >= this.getHeight() - 100 && touchY <= this.getHeight()){ 
     if (action == MotionEvent.ACTION_DOWN){ 
      starship.setLeftRight(0, 1, true); 
      rightButton.update(touchedrightbutton); 
     } 
     else if (action == MotionEvent.ACTION_UP){ 
      starship.setLeftRight(0, 0, false); 
      rightButton.update(rightbutton); 
     } 
     else if (action == MotionEvent.ACTION_POINTER_DOWN){ 
      starship.setLeftRight(0, 1, true); 
      rightButton.update(touchedrightbutton); 
     } 
     else if (action == MotionEvent.ACTION_POINTER_UP){ 
      starship.setLeftRight(0, 0, false); 
      rightButton.update(rightbutton); 
     } 
    } 

    else if(touchX >= this.getWidth() - shootbutton.getWidth() && touchY >+ this.getHeight() - 100 && touchY < this.getHeight()){ 
     if (action == MotionEvent.ACTION_DOWN){ 

      shootButton.update(shoottouched); 
     } 
     else if (action == MotionEvent.ACTION_UP){ 

      shootButton.update(shootbutton); 
     } 
     else if (action == MotionEvent.ACTION_POINTER_DOWN){ 

      shootButton.update(shoottouched); 
     } 
     else if (action == MotionEvent.ACTION_POINTER_UP){ 

      shootButton.update(shootbutton); 
     } 
    } 

    return true;   
} 

------------------------------------------- ---編輯-----------------------------------------

I能夠解決我的問題,現在一切正常。這是我更新的代碼。希望它能幫助那些遭受與我一樣的問題的人。

@Override 
public boolean onTouchEvent(MotionEvent event){ 

    int action = event.getActionMasked();//event.getAction() & MotionEvent.ACTION_MASK; 
    int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; 
    int pointerID = event.getPointerId(pointerIndex); 
    int pointerCount = event.getPointerCount(); 

    touchX = new int[pointerCount]; 
    touchY = new int[pointerCount]; 

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

     touchX[i] = (int) event.getX(i); 
     touchY[i] = (int) event.getY(i); 

    } 
    switch(action){ 

    case MotionEvent.ACTION_DOWN: 
     if(touchX[0] >= 0 && touchX[0] <= leftbutton.getWidth() && touchY[0] >= this.getHeight() - 100 && touchY[0] <= this.getHeight()){ 
      starship.setLeftRight(1, 0, true); 
      leftButton.update(touchedleftbutton); 

     } 
     else if (touchX[0] >= 20 + leftbutton.getWidth() && touchX[0] <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY[0] >= this.getHeight() - 100 && touchY[0] <= this.getHeight()){ 

      starship.setLeftRight(0, 1, true); 
      rightButton.update(touchedrightbutton); 
     } 
     else if(touchX[0] >= this.getWidth() - shootbutton.getWidth() && touchY[0] >+ this.getHeight() - 100 && touchY[0] < this.getHeight()){ 
      shootButton.update(shoottouched); 
     } 

     break; 

    case MotionEvent.ACTION_UP: 
     if(touchX[0] >= 0 && touchX[0] <= leftbutton.getWidth() && touchY[0] >= this.getHeight() - 100 && touchY[0] <= this.getHeight()){ 
      starship.setLeftRight(0, 0, false); 
      leftButton.update(leftbutton); 

     } 
     else if (touchX[0] >= 20 + leftbutton.getWidth() && touchX[0] <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY[0] >= this.getHeight() - 100 && touchY[0] <= this.getHeight()){ 

      starship.setLeftRight(0, 0, false); 
      rightButton.update(rightbutton); 
     } 
     else if(touchX[0] >= this.getWidth() - shootbutton.getWidth() && touchY[0] >+ this.getHeight() - 100 && touchY[0] < this.getHeight()){ 
      shootButton.update(shootbutton); 
     } 

     break; 

    case MotionEvent.ACTION_POINTER_DOWN: 
     if(touchX[pointerID] >= 0 && touchX[pointerID] <= leftbutton.getWidth() && touchY[pointerID] >= this.getHeight() - 100 && touchY[pointerID] <= this.getHeight()){ 
      starship.setLeftRight(1, 0, true); 
      leftButton.update(touchedleftbutton); 

     } 
     else if (touchX[pointerID] >= 20 + leftbutton.getWidth() && touchX[pointerID] <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY[pointerID] >= this.getHeight() - 100 && touchY[pointerID] <= this.getHeight()){ 

      starship.setLeftRight(0, 1, true); 
      rightButton.update(touchedrightbutton); 
     } 
     else if(touchX[pointerID] >= this.getWidth() - shootbutton.getWidth() && touchY[pointerID] >+ this.getHeight() - 100 && touchY[pointerID] < this.getHeight()){ 
      shootButton.update(shoottouched); 
     } 

     break; 

    case MotionEvent.ACTION_POINTER_UP: 
     if(touchX[pointerID] >= 0 && touchX[pointerID] <= leftbutton.getWidth() && touchY[pointerID] >= this.getHeight() - 100 && touchY[pointerID] <= this.getHeight()){ 
      starship.setLeftRight(0, 0, false); 
      leftButton.update(leftbutton); 

     } 
     else if (touchX[pointerID] >= 20 + leftbutton.getWidth() && touchX[pointerID] <= 20 + leftbutton.getWidth() + rightbutton.getWidth() && touchY[pointerID] >= this.getHeight() - 100 && touchY[pointerID] <= this.getHeight()){ 

      starship.setLeftRight(0, 0, false); 
      rightButton.update(rightbutton); 
     } 
     else if(touchX[pointerID] >= this.getWidth() - shootbutton.getWidth() && touchY[pointerID] >+ this.getHeight() - 100 && touchY[pointerID] < this.getHeight()){ 
      shootButton.update(shootbutton); 
     } 

     break; 
    } 
    return true;   
} 
+0

如果您彙總所做的更改這將有助於使用多點觸控。 –

回答

2

看到這個link,它展示瞭如何以適當的方式

+0

感謝您的鏈接。我解決了我的問題。 :) – pslayer89

2

我的編碼Android的多點觸控和測試在一個連接中的經驗令人沮喪。

我結束了使用android-multitouch-controller。那個圖書館會給你一個更好的想法來處理所有的注意事項。

+0

android sdk的多點觸控有什麼問題? – pslayer89

+0

當我將3個手指(或更多)放到屏幕上時,我的代碼被忽略。我有一些代碼可以跟蹤觸摸屏幕上的一個圓圈。就像第三個被觸動一樣。無論你做什麼,它都會停下來。即使你擡起1或2個手指離開2或1個手指。主要的問題是有時它將1個手指分類爲3.因此它會隨機凍結。 – Doomsknight