[英]Android - Retro Squash Game - racket/screen sides collision
I am reading "Learning Java by Building Android Games" and in a Retro Squash Game example I don't know how to implement collision detection for the racket. 我正在阅读“通过构建Android游戏学习Java”,并且在Retro Squash Game示例中,我不知道如何为球拍实现碰撞检测。 The movement of the racket uses onTouchEvent. 球拍的移动使用onTouchEvent。 I have tried to implement an if statement but it deosn't get checked until the next touch event- so it doesn't work properly. 我试图实现一个if语句,但是直到下一次触摸事件才会被检查-因此它无法正常工作。 Please help. 请帮忙。
//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;
}
Ok. 好。 I have found the solution. 我找到了解决方案。 I wasn't looking at the right piece of code - sorry. 我没有查看正确的代码-抱歉。 I have edited the piece which is responsible for changing the racketPoint.x of the racket. 我已经编辑了负责更改球拍的racketPoint.x的片段。 Here it is: 这里是:
public void updateCount() {
//change the racket position according to the movement
if (racketIsMovingRight && (racketPosition.x+racketWidth/2)<=screenWidth) {
racketPosition.x += racketSpeed;
}
if (racketIsMovingLeft && (racketPosition.x-racketWidth/2)>=0) {
racketPosition.x -= racketSpeed;
}
//rest of the code //其余的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.