2
我正在阅读“通过构建Android游戏学习Java”以及复古壁球游戏示例,我不知道如何实现球拍碰撞检测。球拍的运动使用onTouchEvent。我试图实现一个if语句,但是直到下一个触摸事件才会被检查,所以它无法正常工作。请帮忙。Android - 复古壁球游戏 - 球拍/屏幕碰撞
//Event that handles in which direction is the racket moving according to where is the user touching the screen
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
//gets the movement action without the pointers(ACTION_MASK) ??? -U: handles multitouch
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
//what happens if user touches the screen and holds
case MotionEvent.ACTION_DOWN:
//if the screen was touched on the right side of the display than move the racket right
if (motionEvent.getX() >= (screenWidth/2) && racketPosition.x <= screenWidth) {
racketIsMovingLeft = false;
racketIsMovingRight = true;
} else if (motionEvent.getX() < (screenWidth/2) && racketPosition.x >= 0) {
racketIsMovingLeft = true;
racketIsMovingRight = false;
} else {
racketIsMovingLeft = false;
racketIsMovingRight = false;
}
break;
//when the user lets go of the screen the racket immediately stops moving
case MotionEvent.ACTION_UP:
racketIsMovingLeft = false;
racketIsMovingRight = false;
break;
}
return true;
}