The Game is, that the user tries to do a ball in a hole.
1. The touchCounter goes wrong, I want that the user only have 2 tries, when I run that code in my game the user have more tries, what is the mistake?
final int MAX_TRY_COUNT = 2;
int counter = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
float deltaX, deltaY;
float scalingFactor = 1.0f / ((box.xMax > box.yMax) ? box.yMax : box.xMax);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
counter = 2;
if (currentX >= previousX) {
deltaX = currentX - previousX;
ball.speedX += deltaX * scalingFactor;
} else if (previousX >= currentX) {
deltaX = previousX - currentX;
ball.speedX += deltaX * scalingFactor;
} if (currentY >= previousY) {
deltaY = currentY - previousY;
ball.speedX += deltaY * scalingFactor;
} else if (previousY >= currentY) {
deltaY = previousY - currentY;
ball.speedY += deltaY * scalingFactor;
}
break;
case MotionEvent.ACTION_UP:
if (counter >= MAX_TRY_COUNT){
// Alert message
return;
}
counter++;
}
// Save current x, y
previousX = currentX;
previousY = currentY;
return true; // Event handled
}
You need two variables for handle it.
final int MAX_TRY_COUNT = 2;
int counter = 0;
In ACTION_DOWN event
If(counter >= MAX_TRY_COUNT){
// Alert message
return;
}
counter++;
And remove touch_counter =1 from ACTION_UP