多点触控Android - 将一半手指的屏幕用于另一手指的一半

时间:2012-05-11 00:03:35

标签: android multi-touch

嗨我正在制作一个分屏游戏,我无法弄清楚如何控制下来。现在,如果只有一个人触摸屏幕,他们工作正常,但一旦多人做,第一个触摸的人是唯一可以控制的人。所以我需要实现多点触控。我无法弄清楚如何做到这一点,所以一旦一个手指向下控制屏幕上半部分的物体,另一个手指可以放下来控制屏幕的下半部分。我该怎么做?

编辑: 我改变了它,它仍然无法工作,虽然有人可以告诉我为什么

我的尝试无效:

  public boolean onTouch(View v, MotionEvent event){
      //needs multitouch
      if(checkInGame()){

                if(event.getY() < this.getMeasuredHeight() / 2){  
                    pointerId1 = event.getPointerId(event.getActionIndex());
                }else{
                    pointerId2 = event.getPointerId(event.getActionIndex());    
                }

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

                    case MotionEvent.ACTION_MOVE:{

                        if(event.getPointerId(event.getActionIndex()) == pointerId1){  
                            Log.d("Touch","Move1");
                              int pointerIndex = event.getActionIndex();
                          int moveX = 0;
                          int moveY = 0;
                          moveX = (int) event.getX(pointerIndex) - sX;
                          moveY = (int) event.getY(pointerIndex) - sY;

                          ship1.setLoc(moveX, moveY);
                          sX = sX + moveX;
                          sY = sY + moveY;
                        }else if(event.getPointerId(event.getActionIndex()) == pointerId2){
                              int pointerIndex = event.getActionIndex();
                            Log.d("Touch","Move2");
                              int moveX = 0;
                              int moveY = 0;
                              moveX = (int) event.getX(pointerIndex) - sX2;
                              moveY = (int) event.getY(pointerIndex) - sY2;

                              ship2.setLoc(moveX, moveY);
                              sX2 = sX2 + moveX;
                              sY2 = sY2 + moveY;
                        }
                          return true;


                    }
                      case MotionEvent.ACTION_DOWN: {

                            if(event.getPointerId(event.getActionIndex()) == pointerId1){
                                Log.d("Touch","Top1");
                                int pointerIndex = event.getActionIndex();
                                sX = (int) event.getX(pointerIndex);
                                sY = (int) event.getY(pointerIndex);
                            }else if(event.getPointerId(event.getActionIndex()) == pointerId2){
                                int pointerIndex = event.getActionIndex();
                                Log.d("Touch","Bottom1");
                                 sX2 = (int) event.getX(pointerIndex);
                                 sY2 = (int) event.getY(pointerIndex);
                            }
                            return true;
                        }

                      case MotionEvent.ACTION_POINTER_DOWN:{
                            if(event.getPointerId(event.getActionIndex()) == pointerId1){
                                Log.d("Touch","Top2");
                                int pointerIndex = event.getActionIndex();
                                sX = (int) event.getX(pointerIndex);
                                sY = (int) event.getY(pointerIndex);
                            }else if(event.getPointerId(event.getActionIndex()) == pointerId2){
                                int pointerIndex = event.getActionIndex();
                                Log.d("Touch","Bottom2");
                                 sX2 = (int) event.getX(pointerIndex);
                                 sY2 = (int) event.getY(pointerIndex);
                            }
                            return true;
                      }


                }

1 个答案:

答案 0 :(得分:3)

您可能希望按ID查找每个指针。在指针向下,检查ID,以便您知道ID为1的指针是控制对象1.给这个Android博客文章一个读取,它可能会有所帮助:http://android-developers.blogspot.co.uk/2010/06/making-sense-of-multitouch.html