游戏是,用户试图在洞中打球。
1。 touchCounter出错了,我希望用户只有2次尝试,当我在游戏中运行该代码时,用户有更多的尝试,这是什么错误?
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
}
答案 0 :(得分:0)
你需要两个变量来处理它。
final int MAX_TRY_COUNT = 2;
int counter = 0;
在ACTION_DOWN事件中
If(counter >= MAX_TRY_COUNT){
// Alert message
return;
}
counter++;
从ACTION_UP中删除touch_counter = 1