如何处理多点触控?

时间:2014-12-16 14:27:17

标签: 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矩形,那么角色会向右移动,当我移开手指时,他会按预期进行操作。问题是,如果我在触摸其他按钮的同时触摸按钮,它就不会对它做出反应。 所以我想我的问题是,我如何修改我的代码,以便对多点触控作出反应?

谢谢! :)

1 个答案:

答案 0 :(得分:1)

这是一个简单的代码。

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;
}
祝你好运。 :)