java - 多点触控代码不起作用

标签 java android

所以我正在开发 Android 游戏,它需要使用多点触控。我已经阅读了一些有关多点触控的教程并尝试在我的游戏中使用它。 这是我的代码:

    public boolean onTouch(MotionEvent e) {
    int pointerCount = e.getPointerCount();

    for (int i = 0; i < pointerCount; i++) {
        int x = (int) e.getX(i);
        int y = (int) e.getY(i);
        int action = e.getActionMasked();

        for (int j = 0; j < object.size(); j++) {
            tempObject = object.get(j);
            if (tempObject.getId() == ObjectId.Player) {

                switch (action) {

                case MotionEvent.ACTION_UP:
                    if (moveLeft.contains(x, y)) {
                        tempObject.setMovingLeft(false);

                    }
                    if (moveRight.contains(x, y)) {
                        tempObject.setMovingRight(false);

                    }

                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    if (moveLeft.contains(x, y)) {
                        tempObject.setMovingLeft(false);

                    }
                    if (moveRight.contains(x, y)) {
                        tempObject.setMovingRight(false);

                    }

                    break;

                case MotionEvent.ACTION_DOWN:
                    if (jump.contains(x, y)) {
                        if (tempObject.getVelY() == 0 && tempObject.isJumping() == false) {
                            tempObject.setVelY((float) -11.5);
                            tempObject.setJumping(true);
                        }
                    }
                    if (restart.contains(x, y)) {
                        restart();

                    }

                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    if (jump.contains(x, y)) {
                        if (tempObject.getVelY() == 0 && tempObject.isJumping() == false) {
                            tempObject.setVelY((float) -11.5);
                            tempObject.setJumping(true);
                        }
                    }
                    if (restart.contains(x, y)) {
                        restart();

                    }

                    break;

                case MotionEvent.ACTION_MOVE:

                    if (moveLeft.contains(x, y)) {
                        tempObject.setMovingLeft(true);
                        tempObject.setMovingRight(false);

                    }
                    if (moveLeftExit.contains(x, y) && !moveLeft.contains(x, y)) {
                        tempObject.setMovingLeft(false);

                    }
                    if (moveRight.contains(x, y)) {
                        tempObject.setMovingRight(true);
                        tempObject.setMovingLeft(false);

                    }
                    if (moveRightExit.contains(x, y) && !moveRight.contains(x, y)) {
                        tempObject.setMovingRight(false);

                    }
                    break;
                }
            }
        }

    }
    return true;
}

编辑: 我在混合 for 循环变量时犯了一个错误,现在它不会崩溃,但触摸不起作用。

编辑2: 我现在注意到多点触控可以工作,但是当我按下与矩形不同的位置时,它就会被触发。

编辑3: 现在好的部分是多点触控可以工作,因为我可以按两个按钮,它们都会使用react。 不好的部分是,当我触摸不同的较小的地方时,它们会使用react。 希望您能从这张图中明白:

enter image description here

黑色 - 应该触发触摸事件的地方

黄色-触发触摸事件的位置。

谢谢!笑脸

最佳答案

这看起来很奇怪:

for (int j = 0; i < object.size(); j++) {

混淆了 i 和 j ?这可能会导致索引越界错误

关于java - 多点触控代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27743819/

相关文章:

java - Spring Boot - Jackson 在使用列表字段时无法 POST/PUT (@ManyToMany)

java - 如何在android keystore 中存储私钥?

android - android中的运动事件

java - 如何以编程方式隐藏 View ?

java - 应用程序长时间不活动后自动短信不起作用

Android Studio 代理设置构建错误

UNIX 中的 java.lang.NoClassDefFoundError

Java正则表达式性能问题

java - map 卡住,添加到 slider 选项卡后不允许缩放

android - 如何发布隐藏在 Google Play 商店中的应用程序?