java - 如何处理多点触控?

标签 java android game-engine

我正在为 Android 制作一个平台游戏,我需要一些触摸事件方面的帮助。 这是我的代码:

    public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
    for (int i = 0; i < object.size(); i++) {
        tempObject = object.get(i);
        if (tempObject.getId() == ObjectId.Player) {

            if (e.getAction() == MotionEvent.ACTION_MOVE) {

                if (moveLeft.contains(scaledX, scaledY)) {
                    tempObject.setMovingLeft(true);
                    tempObject.setMovingRight(false);

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

                }
                if (moveRight.contains(scaledX, scaledY)) {
                    tempObject.setMovingRight(true);
                    tempObject.setMovingLeft(false);

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

                }

            }
            if (e.getAction() == MotionEvent.ACTION_UP
                    || e.getAction() == MotionEvent.ACTION_OUTSIDE) {

                if (moveLeft.contains(scaledX, scaledY)) {
                    tempObject.setMovingLeft(false);

                }
                if (moveRight.contains(scaledX, scaledY)) {
                    tempObject.setMovingRight(false);

                }
            }

            if (e.getAction() == MotionEvent.ACTION_DOWN) {

                if (jump.contains(scaledX, scaledY)) {
                    if(tempObject.getVelY() ==0)
                    tempObject.setVelY(-15);
                }
            }

        }
    }
    return true;
}

当我使用一根手指时,一切都很好,如果我触摸 moveRight 矩形,角色会向右移动,当我将手指移开时,他会按预期停止。问题是,如果我在触摸其他按钮的同时触摸一个按钮,它不会对此使用react。 所以我想我的问题是,如何修改我的代码以使其对多点触控使用react?

谢谢!! :)

最佳答案

这是一个简单的代码。

public boolean onTouch(View v, MotionEvent event) {

    int action = MotionEventCompat.getActionMasked(event);
    int pointerIndex = MotionEventCompat.getActionIndex(event);
    int x = (int)MotionEventCompat.getX(event,pointerIndex);
    int y = (int)MotionEventCompat.getY(event,pointerIndex);

    switch(action)
    {
    case MotionEvent.ACTION_DOWN:
        //
        // First finger was touched. Set "pointerIndex" to some value (lets say 0 or -1)
        // Save "pointerIndex" corresponding to this touched object.
        //
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        //
        // More finger touched when first finger is already touched.
        // Save "pointerIndex" corresponding to this touched object.
        //
        break;
    case MotionEvent.ACTION_MOVE:
        //
        // Finger with "pointerIndex" was moved.
        //
        break;
    case MotionEvent.ACTION_UP:
        //
        // The first touched finger went off.
        //
        break;
    case MotionEvent.ACTION_POINTER_UP:
        //
        // Finger with "pointerIndex" went off (other than first finger)
        //
        break;
    }
    return true;
}

祝你好运。 :)

关于java - 如何处理多点触控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506882/

相关文章:

java - Simplexml valuerequired 异常

android - BroadcastReceiver 不触发

java - Android GPS 位置追踪器

java - ProGuard 在具有 OpenCSV 依赖项的 Android 上构建失败

user-interface - 如何在 Unity 中针对所有屏幕尺寸正确缩放文本?

java - List<List<T>> 和 List<T> 是否具有相同的原始类型?

java - 在 Main[JAVA] 中用另一种类型的对象替换对象

java - 我希望我的警报对话框能够触发并保持显示错误消息

c++ - 使用固定时间步移动 Sprite

java - LWJGL - 当我的玩家与多个墙壁物体碰撞时,他开始来回弹跳