使用onTouchEvent android进行多点触控的错误坐标

时间:2015-12-02 06:38:56

标签: android ontouchevent

我的游戏输入有问题。在我的游戏中,有一个操纵杆和两个按钮。我已经覆盖了onTouchEvent方法以进行输入。 onTouchEvent更改了类中另一个方法处理的一些变量。

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean touched = false;
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_DOWN:
            touched = true;
            if ((event.getX() < Constants.JOYSTICK_SIZE * Data.density) && (event.getY() > (Data.height - Constants.JOYSTICK_SIZE * Data.density))) {
                xPos = Math.round(event.getX());
                yPos = Math.round(event.getY());
                joystickPressed = true;
            }
            if ((event.getX() > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY() > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                button1Pressed = true;
            }
            if ((event.getX() > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY() > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY() < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                button2Pressed = true;
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            touched = true;
            if ((event.getX(event.getActionIndex()) < Constants.JOYSTICK_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.JOYSTICK_SIZE * Data.density))) {
                xPos = Math.round(event.getX());
                yPos = Math.round(event.getY());
                joystickPressed = true;
            }
            if ((event.getX(event.getActionIndex()) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                button1Pressed = true;
            }
            if ((event.getX(event.getActionIndex()) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY() < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                button2Pressed = true;
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if ((event.getX() > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY() > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                button1Pressed = false;
            } else if ((event.getX() > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY() > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY() < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                button2Pressed = false;
            } else {
                xPos = Math.round(Constants.JOYSTICK_SIZE / 2 * Data.density);
                yPos = Math.round(Data.width - Constants.JOYSTICK_SIZE / 2 * Data.density);
                joystickPressed = false;
            }
            break;
        case MotionEvent.ACTION_POINTER_UP:
            if ((event.getX(event.getActionIndex()) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                button1Pressed = false;
            } else if ((event.getX(event.getActionIndex()) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY() < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                button2Pressed = false;
            } else {
                xPos = Math.round(Constants.JOYSTICK_SIZE / 2 * Data.density);
                yPos = Math.round(Data.width - Constants.JOYSTICK_SIZE / 2 * Data.density);
                joystickPressed = false;
            }
            break;
    }
    return touched || super.onTouchEvent(event);
}

然而,似乎存在问题。当屏幕上只有一个指针或首先按下操纵杆时,代码工作正常。但是当首先按下其中一个按钮时,代码似乎为操纵杆的xPos和yPos提供了错误的坐标。此外,当操纵杆移动时,它似乎会影响按钮。

我的代码有什么问题?任何帮助/建议将不胜感激。

2 个答案:

答案 0 :(得分:0)

修正了这个问题:

    @Override
public boolean onTouchEvent(MotionEvent event) {
    boolean touched = false;
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            touched = true;
            if ((event.getX() < Constants.JOYSTICK_SIZE * Data.density) && (event.getY() > (Data.height - Constants.JOYSTICK_SIZE * Data.density))) {
                xPos = Math.round(event.getX());
                yPos = Math.round(event.getY());
                joystickPressed = true;
            }
            if ((event.getX() > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY() > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                shootPressed = true;
            }
            if ((event.getX() > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY() > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY() < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                superpunchPressed = true;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            touched = true;
            for(int i = 0; i < event.getPointerCount(); ++i){
                if ((event.getX(i) < Constants.JOYSTICK_SIZE * Data.density) && (event.getY(i) > (Data.height - Constants.JOYSTICK_SIZE * Data.density))) {
                    xPos = event.getX(i);
                    yPos = event.getY(i);
                    joystickPressed = true;
                }
                if ((event.getX(i) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(i) > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                    shootPressed = true;
                }
                if ((event.getX(i) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(i) > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY(i) < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                    superpunchPressed = true;
                }
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            touched = true;
            if ((event.getX(event.getActionIndex()) < Constants.JOYSTICK_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.JOYSTICK_SIZE * Data.density))) {
                xPos = event.getX(event.getActionIndex());
                yPos = event.getY(event.getActionIndex());
                joystickPressed = true;
            }
            if ((event.getX(event.getActionIndex()) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                shootPressed = true;
            }
            if ((event.getX(event.getActionIndex()) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY(event.getActionIndex()) < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                superpunchPressed = true;
            }
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if ((event.getX() > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY() > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                shootPressed = false;
            } else if ((event.getX() > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY() > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY() < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                superpunchPressed = false;
            } else {
                xPos = Constants.JOYSTICK_SIZE / 2 * Data.density;
                yPos = Data.width - Constants.JOYSTICK_SIZE / 2 * Data.density;
                joystickPressed = false;
            }
            break;
        case MotionEvent.ACTION_POINTER_UP:
            if ((event.getX(event.getActionIndex()) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.BUTTON_SIZE * Data.density))) {
                shootPressed = false;
            } else if ((event.getX(event.getActionIndex()) > Data.width - Constants.BUTTON_SIZE * Data.density) && (event.getY(event.getActionIndex()) > (Data.height - Constants.BUTTON_SIZE * Data.density * 2 - 20 * Data.density)) && (event.getY(event.getActionIndex()) < (Data.height - Constants.BUTTON_SIZE * Data.density - 20 * Data.density))) {
                superpunchPressed = false;
            } else {
                xPos = Constants.JOYSTICK_SIZE / 2 * Data.density;
                yPos = Data.width - Constants.JOYSTICK_SIZE / 2 * Data.density;
                joystickPressed = false;
            }
            break;
    }
    return touched || super.onTouchEvent(event);
}

答案 1 :(得分:-1)

你必须在onTouch活动中获得这样的正确接触点。

    float xPos = event.getRawX()-v.getX();
    float yPos = event.getRawY()-v.getY();