Reputation: 53
So I am wroking on game game for android and it requires the use of multi touch. I have read some tutorials about multi touch and tryed to use it in my game. This is my code:
public boolean onTouch(MotionEvent e) {
int pointerCount = e.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
int x = (int) e.getX(i);
int y = (int) e.getY(i);
int action = e.getActionMasked();
for (int j = 0; j < object.size(); j++) {
tempObject = object.get(j);
if (tempObject.getId() == ObjectId.Player) {
switch (action) {
case MotionEvent.ACTION_UP:
if (moveLeft.contains(x, y)) {
tempObject.setMovingLeft(false);
}
if (moveRight.contains(x, y)) {
tempObject.setMovingRight(false);
}
break;
case MotionEvent.ACTION_POINTER_UP:
if (moveLeft.contains(x, y)) {
tempObject.setMovingLeft(false);
}
if (moveRight.contains(x, y)) {
tempObject.setMovingRight(false);
}
break;
case MotionEvent.ACTION_DOWN:
if (jump.contains(x, y)) {
if (tempObject.getVelY() == 0 && tempObject.isJumping() == false) {
tempObject.setVelY((float) -11.5);
tempObject.setJumping(true);
}
}
if (restart.contains(x, y)) {
restart();
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
if (jump.contains(x, y)) {
if (tempObject.getVelY() == 0 && tempObject.isJumping() == false) {
tempObject.setVelY((float) -11.5);
tempObject.setJumping(true);
}
}
if (restart.contains(x, y)) {
restart();
}
break;
case MotionEvent.ACTION_MOVE:
if (moveLeft.contains(x, y)) {
tempObject.setMovingLeft(true);
tempObject.setMovingRight(false);
}
if (moveLeftExit.contains(x, y) && !moveLeft.contains(x, y)) {
tempObject.setMovingLeft(false);
}
if (moveRight.contains(x, y)) {
tempObject.setMovingRight(true);
tempObject.setMovingLeft(false);
}
if (moveRightExit.contains(x, y) && !moveRight.contains(x, y)) {
tempObject.setMovingRight(false);
}
break;
}
}
}
}
return true;
}
EDIT: I had a mistake mixing the for loops varibels, now it doesnt crash but the touch doesnt work.
EDIT2: I have now noticed that the multi touch works, but it gets trigered when I press in a different place than the Rectangles where it should..
EDIT3: Now the good part is that the multi touch work,as in I can press two buttons and they both react. The bad part is that they react when I touch a different and smaller place. I hope you can understand from this picture:
Thanks ! Smiley
Upvotes: 0
Views: 68
Reputation: 523
The Exceptionn was caused by the array list.in which you are trying to access the index which is out of range.
java.lang.IndexOutOfBoundsException: Invalid index 83, size is 83
Upvotes: 0
Reputation: 3529
This looks weird:
for (int j = 0; i < object.size(); j++) {
mixing up i and j ? This might be causing the index out of bounds error
Upvotes: 4