Android无法让两个虚拟操纵杆独立移动,但同时也是如此

时间:2013-10-20 07:48:17

标签: android pointers multi-touch joystick

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

有一个左操纵杆和一个右操纵杆。现在只有一个会一次移动。如果有人能让我走上正轨,我会非常感激,因为我一直在做关于这个问题的噩梦。

1 个答案:

答案 0 :(得分:1)

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

忘记当前使用fingerId的方式。获得ACTION_MOVE后,获取两个指针的x/y并使用它们。类似的东西:

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;