Reading incorectly form array list?

I’m not sure the title is the right description for my error but thats the best I could think of.
Here is my code :


	public boolean onTouch(MotionEvent e, int scaledX, int scaledY, View v) {
		int pointerCount = e.getPointerCount(); 
		for (int j = 0; j < object.size(); j++) {
			tempObject = object.get(j);
			if (tempObject.getId() == ObjectId.Player) { // looking for the player object

				for (int i = 0; i < pointerCount; i++) { //checking all the pointers
					int x = (int) ((e.getX(i) / v.getWidth()) * GameMainActivity.GAME_WIDTH); //scaling
					int y = (int) ((e.getY(i) / v.getHeight()) * GameMainActivity.GAME_HEIGHT);//scaling
					int action = e.getActionMasked();

					switch (action) {

					case MotionEvent.ACTION_UP: // realisng finger to stop moving

						if (moveLeft.contains(x, y)) {
							tempObject.setMovingLeft(false);

						}
						if (moveRight.contains(x, y)) {
							tempObject.setMovingRight(false);

						}
						if (moveLeftExit.contains(x, y)) {
							tempObject.setMovingLeft(false);

						}
						if (moveRightExit.contains(x, y)) {
							tempObject.setMovingRight(false);

						}

						break;
					case MotionEvent.ACTION_POINTER_UP: // realisng finger to stop moving

						if (moveLeft.contains(x, y)) {
							tempObject.setMovingLeft(false);

						}
						if (moveRight.contains(x, y)) {
							tempObject.setMovingRight(false);

						}
						if (moveLeftExit.contains(x, y)) {
							tempObject.setMovingLeft(false);

						}
						if (moveRightExit.contains(x, y)) {
							tempObject.setMovingRight(false);

						}

						break;

					case MotionEvent.ACTION_DOWN:
						if (jump.contains(x, y)) { // jumping
							if (tempObject.getVelY() == 0 && tempObject.isJumping() == false && getLevel() != 19) {
								tempObject.setVelY((float) -11.5 - jumpAdd);
								Assets.playSound(Assets.jump);
								if (getLevel() == 17) {
									jumpAdd += 1;
								}
								tempObject.setJumping(true);

							}
						}

						if (restart.contains(x, y)) {
							restart();

						}

						break;
					case MotionEvent.ACTION_POINTER_DOWN:
						if (jump.contains(x, y)) { // jumping
							if (tempObject.getVelY() == 0 && tempObject.isJumping() == false && getLevel() != 19) {
								tempObject.setVelY((float) -11.5 - jumpAdd);

								Assets.playSound(Assets.jump);
								if (getLevel() == 17) {
									jumpAdd += 1;
								}
								tempObject.setJumping(true);

							}
						}

						if (restart.contains(x, y)) {
							restart();

						}
					break;

					case MotionEvent.ACTION_MOVE:

						if (moveLeftExit.contains(x, y) && !moveLeft.contains(x, y)) {
							tempObject.setMovingLeft(false);

						}
						if (moveLeft.contains(x, y)) {
							tempObject.setMovingLeft(true);
							tempObject.setMovingRight(false);

						}
						if (moveRight.contains(x, y)) {
							tempObject.setMovingRight(true);
							tempObject.setMovingLeft(false);

						}
						if (moveRightExit.contains(x, y) && !moveRight.contains(x, y)) {
							tempObject.setMovingRight(false);

						}

						if (moveLeft.contains(x, y) && moveRight.contains(x, y)) {
							tempObject.setMovingLeft(false);
							tempObject.setMovingRight(false);
						}

						break;
					}
				}
			}

		}
		return true;
	}


In the game I have an array list of all the object and when an object is created it gets an ID so I will know which object I am using.
The problem is that somtimes when I press the jump button some other object is jumping instead of the player, or when I realese the finger from the movement button the player wont stop. To me it looks like it reads the ID incorrectly and uses a different object(makes other objects jump and etc…)
I am using the same method to use objects in other parts of my code to check collision and etc and the problem seems to happen only in the touch events.
Any ideas? Thanks !

Why don’t you keep around a reference to your player object instead of searching for it every time you want to move it?

I thought about it but I coudnt find a way how to do it because in each level the player gets created in a different place in the array list…

Whenever you add it to the ArrayList, update the reference variable?

I have no idea why I didnt thought of that, now it works great thanks!
If anyone is interested this is the working code:


	public boolean onTouch(MotionEvent e, int scaledX, int scaledY, View v) {
		int pointerCount = e.getPointerCount();
		playerObject = Player.getPlayer();
		for (int i = 0; i < pointerCount; i++) {
			int x = (int) ((e.getX(i) / v.getWidth()) * GameMainActivity.GAME_WIDTH);
			int y = (int) ((e.getY(i) / v.getHeight()) * GameMainActivity.GAME_HEIGHT);
			int action = e.getActionMasked();

			switch (action) {

			case MotionEvent.ACTION_UP:

				if (moveLeft.contains(x, y)) {
					playerObject.setMovingLeft(false);

				}
				if (moveRight.contains(x, y)) {
					playerObject.setMovingRight(false);

				}
				if (moveLeftExit.contains(x, y)) {
					playerObject.setMovingLeft(false);

				}
				if (moveRightExit.contains(x, y)) {
					playerObject.setMovingRight(false);

				}

				break;
			case MotionEvent.ACTION_POINTER_UP:
				if (moveLeft.contains(x, y)) {
					playerObject.setMovingLeft(false);

				}
				if (moveRight.contains(x, y)) {
					playerObject.setMovingRight(false);

				}
				if (moveLeftExit.contains(x, y)) {
					playerObject.setMovingLeft(false);

				}
				if (moveRightExit.contains(x, y)) {
					playerObject.setMovingRight(false);

				}

				break;

			case MotionEvent.ACTION_DOWN:
				if (jump.contains(x, y)) {
					if (playerObject.getVelY() == 0 && playerObject.isJumping() == false && getLevel() != 19) {
						playerObject.setVelY((float) -11.5 - jumpAdd);

						Assets.playSound(Assets.jump);
						if (getLevel() == 17) {
							jumpAdd += 1;
						}
						playerObject.setJumping(true);

					}
				}

				if (restart.contains(x, y)) {
					restart();

				}
				if (getLevel() == 21) {
					if (y < 440) {
						addObject(new Block(x / 32 * 32, y / 32 * 32, 7, this, ObjectId.Block));
					}
				}
				if (getLevel() == 23) {
					jumpCount--;
				}
				if (jumpCount <= 0) {
					addObject(new Box(480, 50, ObjectId.Box));
					jumpCount = 100;
				}

				break;
			case MotionEvent.ACTION_POINTER_DOWN:
				if (jump.contains(x, y)) {
					if (playerObject.getVelY() == 0 && playerObject.isJumping() == false && getLevel() != 19) {
						playerObject.setVelY((float) -11.5 - jumpAdd);

						Assets.playSound(Assets.jump);
						if (getLevel() == 17) {
							jumpAdd += 1;
						}
						playerObject.setJumping(true);

					}
				}

				if (restart.contains(x, y)) {
					restart();

				}
				if (getLevel() == 21) {
					if (y < 440) {
						addObject(new Block(x / 32 * 32, y / 32 * 32, 7, this, ObjectId.Block));
					}
				}
				if (getLevel() == 23) {
					jumpCount--;
				}
				if (jumpCount <= 0) {
					addObject(new Box(480, 50, ObjectId.Box));
					jumpCount = 100;
				}

				break;

			case MotionEvent.ACTION_MOVE:

				if (moveLeftExit.contains(x, y) && !moveLeft.contains(x, y)) {
					playerObject.setMovingLeft(false);

				}
				if (moveLeft.contains(x, y)) {
					playerObject.setMovingLeft(true);
					playerObject.setMovingRight(false);

				}
				if (moveRight.contains(x, y)) {
					playerObject.setMovingRight(true);
					playerObject.setMovingLeft(false);

				}
				if (moveRightExit.contains(x, y) && !moveRight.contains(x, y)) {
					playerObject.setMovingRight(false);

				}

				if (moveLeft.contains(x, y) && moveRight.contains(x, y)) {
					playerObject.setMovingLeft(false);
					playerObject.setMovingRight(false);
				}

				break;
			}
		}

		return true;
	}