0

In the code below I'm testing multi touch. When the first finger touch the screen, I get the ID of that finger and if I also touch the screen with a second finger, I get that ID as well. But When I move the fingers, I only get the ID 0 and not any for ID 1, the second finger! I need to pass the id later to an method that stores the X and Y coordinates and I need to know which ID that draws on the screen.

How do I get the correct ID for the movement?

    @Override
public boolean onTouch(View v, MotionEvent event) {
    synchronized (gameLoop) {
        for (int i = 0; i < event.getPointerCount() && i < 2; i++) {

            id = event.getPointerCount();

            // Check if finger touch screen
            if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN || event.getActionMasked() == MotionEvent.ACTION_DOWN)) {

                int j = event.getPointerCount();
                int id = event.getPointerId(i);
                String numDown = Integer.toString(j);
                String sId = Integer.toString(id);
                Log.i("Test", "Number DOWN: " + numDown + " ID: " + sId);
            }

            // Check if finger moves on screen
            if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_MOVE)) {

                int id = event.getPointerId(i);
                String sId = Integer.toString(id);
                Log.i("Test", " Moving ID: " + sId);
            }

            // Check if finger leave screen
            if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP || event.getActionMasked() == MotionEvent.ACTION_UP)) {

                int k = event.getPointerCount();
                int id = event.getPointerId(i);
                String numUp = Integer.toString(k);
                String sId = Integer.toString(id);
                Log.i("Test", "Number UP: " + numUp + " ID: " + sId);
            }
        }
    }
    return true;
}
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159

1 Answers1

1

You didn't need for-loop

public final int getPointerId (int pointerIndex)

Parameters pointerIndex Raw index of pointer to retrieve. Value may be from 0 (the first pointer that is down) to getPointerCount()-1.

First finger (event.getX(0),event.getY(0))

Second finger (event.getX(1),event.getY(1))

Community
  • 1
  • 1
Gina
  • 902
  • 8
  • 15