How to handle multi touch?

Hello,
I’m working on a platfrom game for android and as in most platfrom games the player will need to use multi touch, for example moving right and jumping simultaneous.
This my code:


	public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
		int action = e.getAction() & MotionEvent.ACTION_MASK;
		for (int i = 0; i < object.size(); i++) {
			tempObject = object.get(i);
			if (tempObject.getId() == ObjectId.Player) {

				switch (action) {

				case MotionEvent.ACTION_UP:
					if (moveLeft.contains(scaledX, scaledY)) {
						tempObject.setMovingLeft(false);

					}
					if (moveRight.contains(scaledX, scaledY)) {
						tempObject.setMovingRight(false);

					}

					break;
				case MotionEvent.ACTION_POINTER_UP:

					break;

				case MotionEvent.ACTION_DOWN:
					if (jump.contains(scaledX, scaledY)) {
						if (tempObject.getVelY() == 0 && tempObject.isJumping() == false) {
							tempObject.setVelY((float) -11.5);
							tempObject.setJumping(true);
						}
					}
					if (restart.contains(scaledX, scaledY)) {
						restart();

					}

					break;
				case MotionEvent.ACTION_POINTER_DOWN:

					break;

				case 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);

					}

					break;
				}

			}
		}

		return true;
	}



The problem is if I touch a rect while touching a different rect, the new touch wont react and the old on gets “stuck”.
Thanks :slight_smile:

Someone probably won’t be able to trouble shoot this without knowing how and when these motion events are fired. Currently I’d have to infer what each do, and my inference may not exactly be the case. Care to share what framework or library you are using, so then other users or I could look at its documentation?

Edit: This may be going over my head, and may be solvable by some other developers on the site, but I also do not see how anyone could propose a real fix without also knowing how your object, “tempObject,” is designed (the contents of the movement methods, class structure, etc).

Thanks for answering :slight_smile:
I am not using any framework, just the eclipse and android bundle, and for the motion event I use “android.view.MotionEvent”

Ah, I did not realize, as I have not worked with native Android before, and could not see your imports. Though it would still be beneficial to see the other classes you are using with this. Like how the “setMoving…()” methods are set up, etc.

What each motion event does doesnt really matter, the point is that if I touch the screen with one finger the second one wont triger the motion event action.

I must have misunderstood your question, then. Hopefully someone can answer it! :slight_smile:

Maybe do your cases more like this?


...

    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN: {
        ...
      break;
    }
    case MotionEvent.ACTION_MOVE: { // a pointer was moved
      ...
      break;
    }
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_CANCEL: {
      ...
      break;
    }

...

What I see other people online doing is similar to the following:

  • In touch down, add new pointer to list of current pointers
  • In action move, do logic
  • In touch up, remove the now inactive pointer from the list of current pointers

This is my first android project so I have very basic knowledge in touch events.
How can I handle a list of pointers?(how to add pointers,remove and use them).

Here’s a really good tutorial on handling pointers and multi-touch in Android. Read it, and if you still have questions, of course come back and ask them. :slight_smile:

http://www.vogella.com/tutorials/AndroidTouch/article.html