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 !