Gilmor
Gilmor

Reputation: 53

How to handle multi touch?

I'm making a platfrom game for android and I could use some help with the touch events. This is my code:

    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;
}

Everything works great while I use one finger, if I touch the moveRight rectangle the character moves right and when I move my finger away he stopes as expected. The problem is that if I touch a button while touching some other button it wont react to it. So I guess my question is, how can I modify my code so it will react to multi touch?

Thanks!! :)

Upvotes: 1

Views: 96

Answers (1)

Hemendra Sharma
Hemendra Sharma

Reputation: 1060

Here is a simple code.

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;
}

Good Luck. :)

Upvotes: 1

Related Questions