public boolean onTouchEvent(MotionEvent event) { float x=event.getX(); float y=event.getY(); //开始按钮的点击事件 if (isstart==false&&x>startButtonX&&x<startButtonX+DateImage.start.getWidth()&&y>startButtonY&&y<startButtonY+DateImage.start.getHeight()){ isstart=true; } //结束按钮的点击事件 if (isFail&&x>startButtonX&&x<startButtonX+DateImage.start.getWidth()&&y>startButtonY&&y<startButtonY+DateImage.start.getHeight()){ init(); isstart=false; isFail=false; fs=0; } //小鸟向上飞 bird.flappy(); return true; } @Override public void run() { while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } //移动方法 if (isstart&&isFail==false) { ground.move(); column1.move(); column2.move(); bird.move(); //碰撞检测 if (bird.hit(column1,column2,ground)){ isFail=true; } if (bird.pass(column1,column2)){ fs++; } postInvalidate(); } } }这个是什么意思
时间: 2024-02-10 21:35:28 浏览: 20
这段代码是实现游戏的交互和逻辑部分,包括用户触摸屏幕、小鸟飞行、游戏元素移动、碰撞检测等。onTouchEvent()方法用来处理用户在屏幕上的触摸事件,如果用户点击了开始或重启按钮,游戏就会重新开始。同时,如果用户触摸屏幕,小鸟就会向上飞一段距离。
在run()方法中,使用一个while循环不断地更新游戏状态。在每个循环中,游戏元素会移动一定的距离,并进行碰撞检测。如果小鸟撞上了水管或地面,游戏就会结束,isFail会被设置为true。如果小鸟通过了一对水管,游戏得分就会加1。最后,使用postInvalidate()方法通知界面进行重新绘制,以更新游戏状态。同时,在循环中,使用Thread.sleep()方法来让游戏以一定的速度运行,避免过快或过慢的情况。
相关问题
以下代码为何两根手指放在图片上就闪退 请完善 修改 public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: // 手指压下屏幕 mode = MODE.DRAG; // 查找被点击的图片 int index = event.getActionIndex(); float x = event.getX(index); float y = event.getY(index); CustomBitmap clickedBitmap = findClickedBitmap(x, y); if (clickedBitmap != null) { // 切换操作对象 _bitmaps.remove(clickedBitmap); _bitmaps.add(clickedBitmap); // 记录ImageView当前的移动位置 currentMatrix.set(clickedBitmap.matrix); clickedBitmap.matrix.set(currentMatrix); clickedBitmap.startPoint.set(x, y); _curCustomBitmap = clickedBitmap; } postInvalidate(); break; case MotionEvent.ACTION_POINTER_DOWN: // 当屏幕上还有触点(手指),再有一个手指压下屏幕 mode = MODE.ZOOM; // 记录位图的旋转角度和缩放倍数 if (_curCustomBitmap == null) { return true; } _curCustomBitmap.oldRotation = rotation(event); _curCustomBitmap.startDis = distance(event); if (_curCustomBitmap.startDis > 10f) { // 获取缩放中心点的坐标 float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); _curCustomBitmap.midPoint.set((x1 + x2) / 2, (y1 + y2) / 2); // 记录ImageView当前的缩放倍数 currentMatrix.set(_curCustomBitmap.matrix); } break; case MotionEvent.ACTION_MOVE: // 手指在屏幕移动,该事件会不断地触发 if (mode == MODE.DRAG) { // 移动图片 if (_curCustomBitmap == null) { return true; } float dx = event.getX() - _curCustomBitmap.startPoint.x; float dy = event.getY() - _curCustomBitmap.startPoint.y; _curCustomBitmap.matrix.set(currentMatrix); _curCustomBitmap.matrix.postTranslate(dx, dy); } else if (mode == MODE.ZOOM) { // 缩放和旋转图片 if (_curCustomBitmap == null) { return true; } float endDis = distance(event); float rotation = rotation(event) - _curCustomBitmap.oldRotation; if (endDis > 10f) { float scale = endDis / _curCustomBitmap.startDis; _curCustomBitmap.matrix.set(currentMatrix); _curCustomBitmap.matrix.postScale(scale, scale, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y); _curCustomBitmap.matrix.postRotate(rotation, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y); } } break; case MotionEvent.ACTION_POINTER_UP: // 有手指离开屏幕,但屏幕还有触点(手指) if (event.getPointerCount() > 1) { mode = MODE.ZOOM; } else { mode = MODE.NONE; } break; } invalidate(); return true; } // 查找被点击的图片 private CustomBitmap findClickedBitmap(float x, float y) { for (CustomBitmap bitmap : _bitmaps) { float[] values = new float[9]; bitmap.matrix.getValues(values); float globalX = values[Matrix.MTRANS_X]; float globalY = values[Matrix.MTRANS_Y]; float width = values[Matrix.MSCALE_X] * bitmap.getBitmap().getWidth(); float height = values[Matrix.MSCALE_Y] * bitmap.getBitmap().getHeight(); RectF rect = new RectF(globalX, globalY, globalX + width, globalY + height); if (rect.contains(x, y)) { return bitmap; } } return null; } // 计算两点之间的距离 private float distance(MotionEvent event) { float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } // 计算两点之间的旋转角度 private float rotation(MotionEvent event) { float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); double radians = Math.atan2(y2 - y1, x2 - x1); return (float) Math.toDegrees(radians); } }
这段代码的问题可能是在 `MotionEvent.ACTION_POINTER_DOWN` 和 `MotionEvent.ACTION_POINTER_UP` 事件中,应该使用 `event.getActionMasked()` 而不是 `event.getAction()`。`event.getAction()` 会返回所有触点的动作,而 `event.getActionMasked()` 只返回当前触点的动作。因此,当你在处理 `ACTION_POINTER_DOWN` 和 `ACTION_POINTER_UP` 事件时,当你想知道哪个触点被按下或抬起时,你需要使用 `event.getActionIndex()`,而不是假设它是第一个触点。
另外,在 `ACTION_MOVE` 事件中,你需要检查触点数量是否大于1,因为只有在有多个触点时才需要进行缩放和旋转操作。如果只有一个触点,则只需要移动图像。
最后,你应该检查 `_curCustomBitmap` 是否为 null,因为在某些情况下,例如在 ACTION_POINTER_DOWN 事件中,没有设置当前的位图,但是在 ACTION_MOVE 事件中,还是会尝试对其进行旋转和缩放操作,这会导致应用程序崩溃。
下面是修改后的代码:
```
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// 手指压下屏幕
mode = MODE.DRAG;
// 查找被点击的图片
int index = event.getActionIndex();
float x = event.getX(index);
float y = event.getY(index);
CustomBitmap clickedBitmap = findClickedBitmap(x, y);
if (clickedBitmap != null) {
// 切换操作对象
_bitmaps.remove(clickedBitmap);
_bitmaps.add(clickedBitmap);
// 记录ImageView当前的移动位置
currentMatrix.set(clickedBitmap.matrix);
clickedBitmap.matrix.set(currentMatrix);
clickedBitmap.startPoint.set(x, y);
_curCustomBitmap = clickedBitmap;
}
postInvalidate();
break;
case MotionEvent.ACTION_POINTER_DOWN:
// 当屏幕上还有触点(手指),再有一个手指压下屏幕
mode = MODE.ZOOM;
// 记录位图的旋转角度和缩放倍数
if (event.getPointerCount() < 2) {
break;
}
if (_curCustomBitmap == null) {
return true;
}
_curCustomBitmap.oldRotation = rotation(event);
_curCustomBitmap.startDis = distance(event);
if (_curCustomBitmap.startDis > 10f) {
// 获取缩放中心点的坐标
float x1 = event.getX(0);
float y1 = event.getY(0);
float x2 = event.getX(1);
float y2 = event.getY(1);
_curCustomBitmap.midPoint.set((x1 + x2) / 2, (y1 + y2) / 2);
// 记录ImageView当前的缩放倍数
currentMatrix.set(_curCustomBitmap.matrix);
}
break;
case MotionEvent.ACTION_MOVE:
// 手指在屏幕移动,该事件会不断地触发
if (_curCustomBitmap == null) {
return true;
}
if (mode == MODE.DRAG) {
// 移动图片
float dx = event.getX() - _curCustomBitmap.startPoint.x;
float dy = event.getY() - _curCustomBitmap.startPoint.y;
_curCustomBitmap.matrix.set(currentMatrix);
_curCustomBitmap.matrix.postTranslate(dx, dy);
} else if (mode == MODE.ZOOM && event.getPointerCount() > 1) {
// 缩放和旋转图片
float endDis = distance(event);
float rotation = rotation(event) - _curCustomBitmap.oldRotation;
if (endDis > 10f) {
float scale = endDis / _curCustomBitmap.startDis;
_curCustomBitmap.matrix.set(currentMatrix);
_curCustomBitmap.matrix.postScale(scale, scale, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y);
_curCustomBitmap.matrix.postRotate(rotation, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y);
}
}
break;
case MotionEvent.ACTION_POINTER_UP:
// 有手指离开屏幕,但屏幕还有触点(手指)
if (_curCustomBitmap == null) {
return true;
}
if (event.getPointerCount() > 2) {
mode = MODE.ZOOM;
} else {
mode = MODE.NONE;
}
break;
}
invalidate();
return true;
}
// 查找被点击的图片
private CustomBitmap findClickedBitmap(float x, float y) {
for (CustomBitmap bitmap : _bitmaps) {
float[] values = new float[9];
bitmap.matrix.getValues(values);
float globalX = values[Matrix.MTRANS_X];
float globalY = values[Matrix.MTRANS_Y];
float width = values[Matrix.MSCALE_X] * bitmap.getBitmap().getWidth();
float height = values[Matrix.MSCALE_Y] * bitmap.getBitmap().getHeight();
RectF rect = new RectF(globalX, globalY, globalX + width, globalY + height);
if (rect.contains(x, y)) {
return bitmap;
}
}
return null;
}
// 计算两点之间的距离
private float distance(MotionEvent event) {
float x1 = event.getX(0);
float y1 = event.getY(0);
float x2 = event.getX(1);
float y2 = event.getY(1);
return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
// 计算两点之间的旋转角度
private float rotation(MotionEvent event) {
float x1 = event.getX(0);
float y1 = event.getY(0);
float x2 = event.getX(1);
float y2 = event.getY(1);
double radians = Math.atan2(y2 - y1, x2 - x1);
return (float) Math.toDegrees(radians);
}
```
以下代码 ,拖动功能正常,但是在用两根手指进行缩放和旋转时闪退,请您完善 。public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: // 手指压下屏幕 mode = MODE.DRAG; // 查找被点击的图片 int index = event.getActionIndex(); float x = event.getX(index); float y = event.getY(index); CustomBitmap clickedBitmap = findClickedBitmap(x, y); if (clickedBitmap != null) { // 切换操作对象 _bitmaps.remove(clickedBitmap); _bitmaps.add(clickedBitmap); // 记录ImageView当前的移动位置 currentMatrix.set(clickedBitmap.matrix); clickedBitmap.matrix.set(currentMatrix); clickedBitmap.startPoint.set(x, y); _curCustomBitmap = clickedBitmap; } postInvalidate(); break; case MotionEvent.ACTION_POINTER_DOWN: // 当屏幕上还有触点(手指),再有一个手指压下屏幕 mode = MODE.ZOOM; // 记录位图的旋转角度和缩放倍数 if (_curCustomBitmap == null) { return true; } _curCustomBitmap.oldRotation = rotation(event); _curCustomBitmap.startDis = distance(event); if (_curCustomBitmap.startDis > 10f) { // 获取缩放中心点的坐标 float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); _curCustomBitmap.midPoint.set((x1 + x2) / 2, (y1 + y2) / 2); // 记录ImageView当前的缩放倍数 currentMatrix.set(_curCustomBitmap.matrix); } break; case MotionEvent.ACTION_MOVE: // 手指在屏幕移动,该事件会不断地触发 if (mode == MODE.DRAG) { // 移动图片 if (_curCustomBitmap == null) { return true; } float dx = event.getX() - _curCustomBitmap.startPoint.x; float dy = event.getY() - _curCustomBitmap.startPoint.y; _curCustomBitmap.matrix.set(currentMatrix); _curCustomBitmap.matrix.postTranslate(dx, dy); } else if (mode == MODE.ZOOM) { // 缩放和旋转图片 if (_curCustomBitmap == null) { return true; } float endDis = distance(event); float rotation = rotation(event) - _curCustomBitmap.oldRotation; if (endDis > 10f) { float scale = endDis / _curCustomBitmap.startDis; _curCustomBitmap.matrix.set(currentMatrix); _curCustomBitmap.matrix.postScale(scale, scale, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y); _curCustomBitmap.matrix.postRotate(rotation, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y); } } break; case MotionEvent.ACTION_POINTER_UP: // 有手指离开屏幕,但屏幕还有触点(手指) mode = MODE.NONE; break; } invalidate(); return true; } // 查找被点击的图片 private CustomBitmap findClickedBitmap(float x, float y) { for (CustomBitmap bitmap : _bitmaps) { float[] values = new float[9]; bitmap.matrix.getValues(values); float globalX = values[Matrix.MTRANS_X]; float globalY = values[Matrix.MTRANS_Y]; float width = values[Matrix.MSCALE_X] * bitmap.getBitmap().getWidth(); float height = values[Matrix.MSCALE_Y] * bitmap.getBitmap().getHeight(); RectF rect = new RectF(globalX, globalY, globalX + width, globalY + height); if (rect.contains(x, y)) { return bitmap; } } return null; } // 计算两点之间的距离 private float distance(MotionEvent event) { float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } // 计算两点之间的旋转角度 private float rotation(MotionEvent event) { float x1 = event.getX(0); float y1 = event.getY(0); float x2 = event.getX(1); float y2 = event.getY(1); double radians = Math.atan2(y2 - y1, x2 - x1); return (float) Math.toDegrees(radians); }
在处理 MotionEvent.ACTION_POINTER_UP 事件时,需要判断当前屏幕上是否还有触点(手指),如果没有触点了,需要将 mode 设置为 MODE.NONE ,否则会导致下一次触摸事件处理时出现问题。另外,在处理 MotionEvent.ACTION_MOVE 事件时,需要判断当前的 mode 是 MODE.DRAG 还是 MODE.ZOOM,只有在 mode 为 MODE.DRAG 时才需要移动图片,而在 mode 为 MODE.ZOOM 时才需要缩放和旋转图片。完善后的代码如下所示:
```
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// 手指压下屏幕
mode = MODE.DRAG;
// 查找被点击的图片
int index = event.getActionIndex();
float x = event.getX(index);
float y = event.getY(index);
CustomBitmap clickedBitmap = findClickedBitmap(x, y);
if (clickedBitmap != null) {
// 切换操作对象
_bitmaps.remove(clickedBitmap);
_bitmaps.add(clickedBitmap);
// 记录ImageView当前的移动位置
currentMatrix.set(clickedBitmap.matrix);
clickedBitmap.matrix.set(currentMatrix);
clickedBitmap.startPoint.set(x, y);
_curCustomBitmap = clickedBitmap;
}
postInvalidate();
break;
case MotionEvent.ACTION_POINTER_DOWN:
// 当屏幕上还有触点(手指),再有一个手指压下屏幕
mode = MODE.ZOOM;
// 记录位图的旋转角度和缩放倍数
if (_curCustomBitmap == null) {
return true;
}
_curCustomBitmap.oldRotation = rotation(event);
_curCustomBitmap.startDis = distance(event);
if (_curCustomBitmap.startDis > 10f) {
// 获取缩放中心点的坐标
float x1 = event.getX(0);
float y1 = event.getY(0);
float x2 = event.getX(1);
float y2 = event.getY(1);
_curCustomBitmap.midPoint.set((x1 + x2) / 2, (y1 + y2) / 2);
// 记录ImageView当前的缩放倍数
currentMatrix.set(_curCustomBitmap.matrix);
}
break;
case MotionEvent.ACTION_MOVE:
// 手指在屏幕移动,该事件会不断地触发
if (mode == MODE.DRAG) {
// 移动图片
if (_curCustomBitmap == null) {
return true;
}
float dx = event.getX() - _curCustomBitmap.startPoint.x;
float dy = event.getY() - _curCustomBitmap.startPoint.y;
_curCustomBitmap.matrix.set(currentMatrix);
_curCustomBitmap.matrix.postTranslate(dx, dy);
} else if (mode == MODE.ZOOM) {
// 缩放和旋转图片
if (_curCustomBitmap == null) {
return true;
}
float endDis = distance(event);
float rotation = rotation(event) - _curCustomBitmap.oldRotation;
if (endDis > 10f) {
float scale = endDis / _curCustomBitmap.startDis;
_curCustomBitmap.matrix.set(currentMatrix);
_curCustomBitmap.matrix.postScale(scale, scale, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y);
_curCustomBitmap.matrix.postRotate(rotation, _curCustomBitmap.midPoint.x, _curCustomBitmap.midPoint.y);
}
}
break;
case MotionEvent.ACTION_POINTER_UP:
// 有手指离开屏幕,但屏幕还有触点(手指)
if (event.getPointerCount() > 1) {
mode = MODE.ZOOM;
} else {
mode = MODE.NONE;
}
break;
}
invalidate();
return true;
}
```