多点触控:pointerIndex超出范围

时间:2018-06-14 08:24:47

标签: java android

我在Android游戏中处理多点触控有些麻烦: 当我用三个或更多手指触摸屏幕时,将其移除,然后再次触摸它我会收到此错误:

java.lang.IllegalArgumentException: pointerIndex out of range, which occurs in this line of code in onTouchEvent :     x = (int) event.getX(event.findPointerIndex(a));

这是代码

@Override
public boolean onTouchEvent(MotionEvent event)
{
    //touch positions
    int x, y;
    //number of touched areas
    int num = event.getPointerCount();
    //masked touch action
    int action = event.getActionMasked();

    if(1 < num && num < 3)
    {
        for (int a = 0; a < num; a++)
        {
            x = (int) event.getX(event.findPointerIndex(a));
            y = (int) event.getY(event.findPointerIndex(a));
            switch (action)
            {
                case MotionEvent.ACTION_DOWN:
                case  MotionEvent.ACTION_MOVE:
                {
                    if(y > screenY/2)  point1.set(x, y);
                    if(y < screenY/2)  point2.set(x, y);
                }
            }
        }
    }
    else
    {
        x = (int) event.getX();
        y = (int) event.getY();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
            case  MotionEvent.ACTION_MOVE:
            {
                if(y > screenY/2)  point1.set(x, y);
                if(y < screenY/2)  point2.set(x, y);
            }
        }
    }
    return true;
}

0 个答案:

没有答案