2013-06-20 1 views
0

화면에 두 개의 부드러운 d 패드가있는 앱을 쓰고 있습니다. 모든 ACTION_MOVE 이벤트가 id가 0이라는 사실을 알기 때문에 두 개의 터치에 대한 모든 터치 이벤트를 캡처하고 이해하는 데 극도의 어려움을 겪고 있습니다.게임 컨트롤러가있는 Android 멀티 터치 문제

내가 갖고있는 것은 두 개의 '터치'객체입니다. 터치가있을 때 충돌 감지 시스템에 입력합니다. 나는 두 개의 패드가 있으며 두 가지 모두 동시에 제어 할 수 있어야한다는 것을 기억하십시오.

누구에게도 제안 사항이 있습니까? 여기 onTouchEvent 메서드가 있습니다. 컨텍스트를 이해하는 것이 어려울 수도 있다는 것을 명심하십시오. 왜냐하면 저는 d-pads와의 충돌 감지에 cusomt 'touch'게임 객체를 사용하기 때문입니다.

@Override 
public void onTouchEvent(MotionEvent e) 
{ 
    int index = e.getActionIndex(); 


    int action = MotionEventCompat.getActionMasked(e); 
    // Get the index of the pointer associated with the action. 
    //index = MotionEventCompat.getActionIndex(e); 

    int id = e.getPointerId(index); 


    if(touch1.pointerID<0) 
     touch1.pointerID = id; 
    else 
     touch2.pointerID = id; 

    switch(action) 
    { 
     case MotionEvent.ACTION_DOWN: 
     { 
      if(id == touch1.pointerID) 
      { 
       touch1.setCollideable(true); 
       touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio(); 
       touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio(); 
      } 
      else if(id==touch2.pointerID) 
      { 
       touch2.setCollideable(true); 
       touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio(); 
       touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio(); 

      } 

      break; 
     } 
     case MotionEvent.ACTION_POINTER_DOWN: 
     { 
      if(id == touch1.pointerID) 
      { 
       touch1.setCollideable(true); 
       touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio(); 
       touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio(); 
      } 
      else if(id==touch2.pointerID) 
      { 
       touch2.setCollideable(true); 
       touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio(); 
       touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio(); 

      } 
      break; 
     } 
     case MotionEvent.ACTION_UP: 
     { 
      if(id == touch1.pointerID) 
      { 
       touch1.setCollideable(false); 
       touch1.pointerID = -1; 
       touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio(); 
       touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio(); 
      } 
      else if(id==touch2.pointerID) 
      { 
       touch2.setCollideable(false); 
       touch2.pointerID = -1; 
       touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio(); 
       touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio(); 

      } 
      break; 
     } 
     case MotionEvent.ACTION_POINTER_UP: 
     { 
      if(id == touch1.pointerID) 
      { 
       touch1.setCollideable(false); 
       touch1.pointerID = -1; 
       touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio(); 
       touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio(); 
      } 
      else if(id==touch2.pointerID) 
      { 
       touch2.setCollideable(false); 
       touch2.pointerID = -1; 
       touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio(); 
       touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio(); 

      } 
      break; 
     } 
     case MotionEvent.ACTION_MOVE: 
     { 
       Log.d("pointer",String.format("Move index=%s id=%s", index,id)); 

       for(int i=0;i<e.getPointerCount();i++) 
       { 
        Log.d("pointer",String.format("i=%s id=%s",i,e.getPointerId(i))); 

        touch1.setCollideable(true); 
        touch1.getPosition().x = e.getX(i)*GameWorldProperties.getWidthRatio(); 
        touch1.getPosition().y = e.getY(i)*GameWorldProperties.getHeightRatio(); 

        touch2.setCollideable(true); 
        touch2.getPosition().x = e.getX(i)*GameWorldProperties.getWidthRatio(); 
        touch2.getPosition().y = e.getY(i)*GameWorldProperties.getHeightRatio(); 
       } 

      break; 
     } 
     default: 
     { 
      break; 
     } 
    } 

답변

1

희망이 있습니다. 이것은 안드로이드 게임 프로그래밍의 초보자 책입니다.

public boolean onTouch(View v, MotionEvent event) { 
    synchronized (this) { 
     int action = event.getAction() & MotionEvent.ACTION_MASK; 
     int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; 
     int pointerId = event.getPointerId(pointerIndex); 
     TouchEvent touchEvent; 

     switch (action) { 
     case MotionEvent.ACTION_DOWN: 
     case MotionEvent.ACTION_POINTER_DOWN: 
      touchEvent = touchEventPool.newObject(); 
      touchEvent.type = TouchEvent.TOUCH_DOWN; 
      touchEvent.pointer = pointerId; 
      touchEvent.x = touchX[pointerId] = (int) (event 
        .getX(pointerIndex) * scaleX); 
      touchEvent.y = touchY[pointerId] = (int) (event 
        .getY(pointerIndex) * scaleY); 
      isTouched[pointerId] = true; 
      touchEventsBuffer.add(touchEvent); 
      break; 

     case MotionEvent.ACTION_UP: 
     case MotionEvent.ACTION_POINTER_UP: 
     case MotionEvent.ACTION_CANCEL: 
      touchEvent = touchEventPool.newObject(); 
      touchEvent.type = TouchEvent.TOUCH_UP; 
      touchEvent.pointer = pointerId; 
      touchEvent.x = touchX[pointerId] = (int) (event 
        .getX(pointerIndex) * scaleX); 
      touchEvent.y = touchY[pointerId] = (int) (event 
        .getY(pointerIndex) * scaleY); 
      isTouched[pointerId] = false; 
      touchEventsBuffer.add(touchEvent); 
      break; 

     case MotionEvent.ACTION_MOVE: 
      int pointerCount = event.getPointerCount(); 
      for (int i = 0; i < pointerCount; i++) { 
       pointerIndex = i; 
       pointerId = event.getPointerId(pointerIndex); 

       touchEvent = touchEventPool.newObject(); 
       touchEvent.type = TouchEvent.TOUCH_DRAGGED; 
       touchEvent.pointer = pointerId; 
       touchEvent.x = touchX[pointerId] = (int) (event 
         .getX(pointerIndex) * scaleX); 
       touchEvent.y = touchY[pointerId] = (int) (event 
         .getY(pointerIndex) * scaleY); 
       touchEventsBuffer.add(touchEvent); 
      } 
      break; 
     } 

     return true; 
    } 
} 
+0

사실, 정말 도움이됩니다. 이 말은 터치 객체에 대한 참조를 유지하는 대신 각 액션마다 새 객체를 생성해야한다는 것입니다. 말이된다! 감사! – Matthew

+0

당신은 오신 것을 환영합니다. 나는 그 책을 정말로 추천한다. 나는 그것으로 많은 것을 배웠다. – neo