Android MultiTouch仅适用于一根手指

时间:2014-06-03 22:54:14

标签: java android multi-touch

我正在开发一款双人Pong游戏,并希望用一根手指控制蝙蝠。但是,我的问题是它一次只响应一个手指在屏幕上。整个游戏都在一个视图中运行。首先,我检查指针是否处于“活动状态”,然后检查它是否在屏幕的顶部或底部出现,然后再移动相应的蝙蝠。它只能正常工作,只有一个触摸寄存器。我的方法代码如下:

    // variables
    int _touchY = (int) event.getY();
    boolean isTopSide = true;

    // test if motionEvent was on the top or bottom of the screen
    if (SCREEN_HEIGHT / 2 - _touchY < 0) {
        isTopSide = false;
    }

    switch (event.getActionMasked()) {

        case MotionEvent.ACTION_DOWN:
            _activePointer = event.getPointerId(0);
            break;

        case MotionEvent.ACTION_MOVE:
            if(_activePointer != INVALID_POINTER_ID) {
                if (isTopSide) { // top bat
                    _topBat.moveBat(event);
                } else { // bottom bat
                    _botBat.moveBat(event);
                }
            }
            if(_newPointer != INVALID_POINTER_ID){
                if (isTopSide) { // top bat
                    _topBat.moveBat(event);
                } else { // bottom bat
                    _botBat.moveBat(event);
                }
            }
            break;

        case MotionEvent.ACTION_UP:
            if(_activePointer != INVALID_POINTER_ID){
                _activePointer = INVALID_POINTER_ID;
            }
            break;

        case MotionEvent.ACTION_CANCEL:
            _activePointer = INVALID_POINTER_ID;
            break;

        case MotionEvent.ACTION_POINTER_UP:
            int pointerIndex = event.getActionIndex();
            int pointerId = event.getPointerId(pointerIndex);
            if(pointerId == _activePointer){
                _activePointer = INVALID_POINTER_ID;
            }

        case MotionEvent.ACTION_POINTER_DOWN:
            int newPointerIndex = event.getActionIndex();
            _newPointer = event.getPointerId(newPointerIndex);
            break;
    }

相关的全球代码

// touched
private static final int INVALID_POINTER_ID = -1;
private int _activePointer = INVALID_POINTER_ID;
private int _newPointer = INVALID_POINTER_ID;

1 个答案:

答案 0 :(得分:3)

为什么你只看到单个指针的值的主要问题是因为event.getY()没有任何参数会给你第一个指针索引的位置,即使实际移动的指针是第二个指针。 / p>

MotionEvent类包含所有活动指针的坐标。

如果您想获得第二个指针的位置,您需要拨打event.getY(1)

您可能希望遍历所有活动点,通过event.getPointerCount()获取总计数,然后相应地发送事件。