3

嗨,我正在制作一个分屏游戏,但我不知道如何关闭控件。现在,如果只有一个人触摸屏幕,它们就可以正常工作,但是一旦多人触摸,第一个触摸的人就是唯一可以控制的人。所以我需要实现多点触控。我不知道该怎么做,所以一旦一根手指向下控制屏幕上半部分的对象,另一根手指就可以控制屏幕的下半部分。我该怎么做?

编辑:我改变了它,它仍然不起作用,但有人可以告诉我为什么

我的尝试不起作用:

  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;
                      }


                }
4

1 回答 1

3

您可能希望通过其 ID 跟踪每个指针。在指针向下时,检查 ID,以便您知道 ID 为 1 的指针正在控制对象 1。阅读此 Android 博客文章,它可能会有所帮助:http ://android-developers.blogspot.co.uk/2010/06/ multitouch.html 的意义

于 2012-05-11T00:11:38.070 回答