正确处理多点触控事件 android

properly handling multi-touch event android

我在实现多点触控时遇到了问题,我已经搜索了一段时间。当前的实现有效但会中断其他输入。此代码是我的输入控制器的一部分,其中 handles/draws 输入区域。运动事件通过此函数从 OnTouchEvent() 传递, 我读过你必须使用指针 id 来正确跟踪,但每次我尝试实现它时,指针索引都超出范围。 我真的需要一些帮助

 public void handleInput(MotionEvent motionEvent,LevelManager l, SoundManager sound, Viewport vp){
    int pointerCount = motionEvent.getPointerCount();
    for (int i = 0; i < pointerCount; i++) {
        int x = (int) motionEvent.getX(i);
        int y = (int) motionEvent.getY(i);
        if(l.isPlaying()) {

            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    if (right.contains(x, y)) {
                        l.player.setPressingRight(true);
                        l.player.setPressingLeft(false);
                    } else if (left.contains(x, y)) {
                        l.player.setPressingLeft(true);
                        l.player.setPressingRight(false);
                    } else if (jump.contains(x, y)) {
                        l.player.startJump(sound);
                    } else if (shoot.contains(x, y)) {
                        if(l.player.pullTrigger()){
                            sound.playSound("shoot");
                        }
                    } else if (pause.contains(x, y)) {
                        l.switchPlayingStatus();

                    }

                    break;


                case MotionEvent.ACTION_UP:
                    if (right.contains(x, y)) {
                        l.player.setPressingRight(false);
                    } else if (left.contains(x, y)) {
                        l.player.setPressingLeft(false);
                    }


                    break;


                case MotionEvent.ACTION_POINTER_DOWN:
                    if (right.contains(x, y)) {
                        l.player.setPressingRight(true);
                        l.player.setPressingLeft(false);
                    } else if (left.contains(x, y)) {
                        l.player.setPressingLeft(true);
                        l.player.setPressingRight(false);
                    } else if (jump.contains(x, y)) {
                        l.player.startJump(sound);
                    } else if (shoot.contains(x, y)) {
                        if(l.player.pullTrigger()){
                            sound.playSound("shoot");
                        }
                    } else if (pause.contains(x, y)) {
                        l.switchPlayingStatus();
                    }
                    break;


                case MotionEvent.ACTION_POINTER_UP:
                    if (right.contains(x, y)) {
                        l.player.setPressingRight(false);
                    } else if (left.contains(x, y)) {
                        l.player.setPressingLeft(false);
                    } else if (shoot.contains(x, y)) {
                        //Handle shooting here
                    } else if (jump.contains(x, y)) {
                        //Handle more jumping stuff here later
                    }
                    break;
            }
        }else {// Not playing
            //Move the viewport around to explore the map
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    if (right.contains(x, y)) {
                        vp.moveViewportRight(l.mapWidth);
                    } else if (left.contains(x, y)) {
                        vp.moveViewportLeft();
                    } else if (jump.contains(x, y)) {
                        vp.moveViewportUp();
                    } else if (shoot.contains(x, y)) {
                        vp.moveViewportDown(l.mapHeight);
                    } else if (pause.contains(x, y)) {
                        l.switchPlayingStatus();
                    }
                    break;
            }



        }
    }

}

好的,我找到了答案,使用 MotionEventCompat 我能够通过添加索引并通过 MotionEventCompat 接口传入事件来实现所需的行为。

        int index = MotionEventCompat.getActionIndex(motionEvent);
        int x = (int) MotionEventCompat.getX(motionEvent, index);
        int y = (int) MotionEventCompat.getY(motionEvent, index);

而不是 int x = (int) motionEvent.getX(i); int y = (int) motionEvent.getY(i); 紧接着

        for (int i = 0; i < pointerCount; i++) {

老实说,这对我来说似乎是个谜,感觉真的很老套,我什至不确定现在是否有必要遍历两个指针。我会尝试回复评论

//编辑 for 循环肯定是多余的,我只需要在我进一步尴尬之前确定一下