简体   繁体   中英

Android Multi-Touch ACTION_POINTER_UP MotionEvent.getX IllegalArgumentException

Currently I am working on a game that implements Android multi-touch by overriding the onTouchEvent method. In it I have a for loop going over all of the pointers. However, sometimes on ACTION_POINTER_UP this fails to work correctly and throws an IllegalArgumentException . I tried to catch it and continue my loop but it sometimes doesn't work and displays erratic behaviour. I have debugged to make sure when the IllegalArgumentException was thrown the looped pointer was not the active pointer nor was it greater or equal to the total number of pointers. Here's my code for reference.

@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
            return true;
        }
        if (event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
            return false;
        }
        boolean notUp = true;
        int pointerCount = event.getPointerCount();
        boolean p1Turned = false;
        boolean p2Turned = false;
        for (int i = 0; i < pointerCount; ++i) {
            float x, y;
            if (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP && event.getActionIndex() == i){
                notUp = false;
                continue;
            }
            try {
                x = event.getX(event.getPointerId(i));
                y = D.height - event.getY(event.getPointerId(i));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                continue;
            }
            //test if the pointer is touching a button
            if (x >= buttons[0] && x <= buttons[6] && y <= buttons[1] && y >= buttons[7]) {
                p1.turnLeft();
                p1Turned = true;
            } else if (x >= buttons[12] && x <= buttons[12 + 6] && y <= buttons[12 + 1] && y >= buttons[12 + 7]) {
                p1.turnRight();
                p1Turned = true;
            } else if (x >= buttons[24] && x <= buttons[24 + 6] && y <= buttons[24 + 1] && y >= buttons[24 + 7]) {
                p2.turnLeft();
                p2Turned = true;
            } else if (x >= buttons[36] && x <= buttons[36 + 6] && y <= buttons[36 + 1] && y >= buttons[36 + 7]) {
                p2.turnRight();
                p2Turned = true;
            }
        }
        if (!p1Turned) {
            p1.stopTurning();
        }
        if (!p2Turned) {
            p2.stopTurning();
        }
        if (event.getActionMasked() == MotionEvent.ACTION_UP){
            p1.stopTurning();
            p2.stopTurning();
        }
        return notUp;
    } 

When the glitch occurs it's always ACTION_POINTER_UP and through debugging I found when the issue occurs event.getActionIndex() is always 0, however when i = 1 and event.getX(event.getPointerId(i)) is called it throws an IllegalArgumentException and the loop is skipped over a second time. The number of active pointers is always 2 when this situation occurs so no code in the for block gets called.

Any help regarding the situation would be appreciated.

Never mind, event.getX() takes in the index, not the ID. See this for more reference: Android/Processing MotionEvent getX() raises exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM