Android 安卓可以';不要让两个虚拟操纵杆同时独立移动 @覆盖 公共布尔onTouch(视图v,运动事件){ //TODO自动生成的方法存根 浮点数r=70; 浮动中心lx=(浮动)(屏幕宽度*.3425); 中心浮动=(浮动)(屏幕高度*.4958); 浮动中心rx=(浮动)(屏幕宽度*.6538); 浮动中心=(浮动)(屏幕高度*.4917); 浮点数dx=0; 浮动dy=0; 浮动θ; 浮点数c; int action=event.getAction(); int actionCode=action&MotionEvent.action\u掩码; int pid=(action&MotionEvent.action\u POINTER\u INDEX\u MASK)>>MotionEvent.action\u POINTER\u INDEX\u SHIFT; int fingerid=event.getPointerId(pid); intx=(int)event.getX(pid); int y=(int)event.getY(pid); c=FloatMath.sqrt(dx*dx+dy*dy); θ=(float)Math.atan(Math.abs(dy/dx)); 开关(动作码){ case MotionEvent.ACTION\u DOWN: case MotionEvent.ACTION\u指针\u向下: //如果触碰左杆,将leftstick ID设置为该fingerid。 如果(x0&&dy=r&&touchingRs&&fingerid==rsId){ 如果(dx>0&&dy

Android 安卓可以';不要让两个虚拟操纵杆同时独立移动 @覆盖 公共布尔onTouch(视图v,运动事件){ //TODO自动生成的方法存根 浮点数r=70; 浮动中心lx=(浮动)(屏幕宽度*.3425); 中心浮动=(浮动)(屏幕高度*.4958); 浮动中心rx=(浮动)(屏幕宽度*.6538); 浮动中心=(浮动)(屏幕高度*.4917); 浮点数dx=0; 浮动dy=0; 浮动θ; 浮点数c; int action=event.getAction(); int actionCode=action&MotionEvent.action\u掩码; int pid=(action&MotionEvent.action\u POINTER\u INDEX\u MASK)>>MotionEvent.action\u POINTER\u INDEX\u SHIFT; int fingerid=event.getPointerId(pid); intx=(int)event.getX(pid); int y=(int)event.getY(pid); c=FloatMath.sqrt(dx*dx+dy*dy); θ=(float)Math.atan(Math.abs(dy/dx)); 开关(动作码){ case MotionEvent.ACTION\u DOWN: case MotionEvent.ACTION\u指针\u向下: //如果触碰左杆,将leftstick ID设置为该fingerid。 如果(x0&&dy=r&&touchingRs&&fingerid==rsId){ 如果(dx>0&&dy,android,pointers,multi-touch,joystick,Android,Pointers,Multi Touch,Joystick,我看到的主要问题是,当你收到一个动作时,你只处理一个指针。Android通常会将多个事件批处理成一个MotionEvent,因此除非你同时更新两个指针,否则你将无法同时进行两个移动 忘记您当前使用的fingerId的方式。当您获得动作\u MOVE时,获取两个指针的x/y,并使用它们。例如: @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated met

我看到的主要问题是,当你收到一个
动作时,你只处理一个指针。Android通常会将多个事件批处理成一个
MotionEvent
,因此除非你同时更新两个指针,否则你将无法同时进行两个移动

忘记您当前使用的
fingerId
的方式。当您获得
动作\u MOVE
时,获取两个指针的
x/y
,并使用它们。例如:

   @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        float r = 70;
        float centerLx = (float) (screenWidth*.3425);
        float centerLy = (float) (screenHeight*.4958);
        float centerRx = (float) (screenWidth*.6538);
        float centerRy = (float) (screenHeight*.4917);
        float dx = 0;
        float dy = 0;
        float theta;
        float c;

        int action = event.getAction(); 
        int actionCode = action & MotionEvent.ACTION_MASK; 
        int pid = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        int fingerid = event.getPointerId(pid);
        int x = (int) event.getX(pid);
        int y = (int) event.getY(pid);




            c = FloatMath.sqrt(dx*dx + dy*dy);
            theta = (float) Math.atan(Math.abs(dy/dx));


            switch (actionCode) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:

                //if touching down on left stick, set leftstick ID to this fingerid.                                                             
                if(x < screenWidth/2 && c<r*.8) {
                    lsId = fingerid;
                                    dx = x-centerLx;
                        dy = y-centerLy;
                    touchingLs = true;
                }
                else if(x > screenWidth/2 && c<r*.8) {
                    rsId = fingerid;
                                    dx = x-centerRx;
                                    dy = y-centerRy;
                    touchingRs = true;
                }



                    break;
            case MotionEvent.ACTION_MOVE:


            if (touchingLs && fingerid == lsId) { 
                dx = x - centerLx;
                dy = y - centerLy;
            }else if (touchingRs && fingerid == rsId) { 
                dx = x - centerRx;
                dy = y - centerRy;
            }


                c = FloatMath.sqrt(dx*dx + dy*dy);
                theta = (float) Math.atan(Math.abs(dy/dx));


                //if touching outside left radius and moving left stick
                if(c >= r && touchingLs && fingerid == lsId) {
                    if(dx>0 && dy<0) { //top right quadrant
                        lsX = r * FloatMath.cos(theta);
                        lsY = -(r * FloatMath.sin(theta));
                        Log.i("message", "top right");
                    }

                    if(dx<0 && dy<0) { //top left quadrant
                        lsX = -(r * FloatMath.cos(theta));
                        lsY = -(r * FloatMath.sin(theta));
                        Log.i("message", "top left");
                    }

                    if(dx<0 && dy>0) { //bottom left quadrant
                        lsX = -(r * FloatMath.cos(theta));
                        lsY = r * FloatMath.sin(theta);
                        Log.i("message", "bottom left");
                    }


                    else if(dx > 0 && dy > 0){ //bottom right quadrant
                        lsX = r * FloatMath.cos(theta);
                        lsY = r * FloatMath.sin(theta);
                        Log.i("message", "bottom right");
                    }


                }
                if(c >= r && touchingRs && fingerid == rsId) {
                    if(dx>0 && dy<0) { //top right quadrant
                        rsX = r * FloatMath.cos(theta);
                        rsY = -(r * FloatMath.sin(theta));
                        Log.i("message", "top right");
                    }
                    if(dx<0 && dy<0) { //top left quadrant
                        rsX = -(r * FloatMath.cos(theta));
                        rsY = -(r * FloatMath.sin(theta));
                        Log.i("message", "top left");
                    }
                    if(dx<0 && dy>0) { //bottom left quadrant
                        rsX = -(r * FloatMath.cos(theta));
                        rsY = r * FloatMath.sin(theta);
                        Log.i("message", "bottom left");
                    }
                    else if(dx > 0 && dy > 0) {
                        rsX = r * FloatMath.cos(theta);
                        rsY = r * FloatMath.sin(theta);
                        Log.i("message", "bottom right");
                    }
                }
                else {
                    if(c < r && touchingLs && fingerid == lsId) {
                        lsX = dx;
                        lsY = dy;
                    }
                    if(c < r && touchingRs && fingerid == rsId){
                        rsX = dx;
                        rsY = dy;
                    }

                } 

                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:

            if (fingerid == lsId) { 
                lsId = -1;
                lsX = 0;
                lsY = 0;
                touchingLs = false;
            } else if (fingerid == rsId) { 
                rsId = -1;
                rsX = 0;
                rsY = 0;
                touchingRs = false;
            }


                break;
            }



        return true;
    }
case ACTION_MOVE:
    int x;
    int y;

    int leftIndex = event.findPointerIndex(lsId);
    x = (int)event.getX(leftIndex);
    y = (int)event.getY(leftIndex);
    /// ... do stuff with left coordinates
    ...
    int rightIndex = event.findPointerIndex(rsId);
    x = (int)event.getX(rightIndex);
    y = (int)event.getY(rightIndex);
    /// ... do stuff with right coordinates
    ...
    break;