onTouchEvent()无法正常工作

时间:2015-06-29 10:52:25

标签: java android ontouchevent

以下:https://developer.android.com/training/gestures/movement.html

用于处理$orderdate = '20150102'; if (preg_match('#^(\d{4})(\d{2})(\d{2})$#', $orderdate, $matches)) { $day = $matches[1]; $month = $matches[2]; $year = $matches[3]; } 中的移动。我的问题是,即使按照教程链接,似乎从未注册过触摸。

代码:

$date = "20160101";

$array['year']  = substr($date, 0, 4);
$array['month'] = substr($date, 4, 2);
$array['day']   = substr($date, 6, 2);

print_r($array);

每次触摸精灵时都会收到一条logcat消息,但每当我触摸屏幕或滑动时我什么也得不到。我意识到最可能的原因是我没有正确使用onTouchEvent,但我已尝试实施其他问题中建议的一些事情,但没有运气。

修改

如下所示,我的游戏逻辑在这里是错误的。我在一个错误的类中使用了onTouchEvent。当我将其更改为public class Sprite extends Activity { private static final int BMP_ROWS = 4; private static final int BMP_COL = 3; private int x, y = 0; private int speedx, speedy = 5; private int currentFrame = 0; private int width, height; private float xTouch, yTouch; private VelocityTracker mVelocityTracker = null; private GameView gameView; private Bitmap bmp; int [] DIRECTION_TO_ANIMATE_MAP = {3, 1, 0, 2}; public Sprite(GameView gameView, Bitmap bmp) { this.gameView = gameView; this.bmp = bmp; this.width = bmp.getWidth() / BMP_COL; this.height = bmp.getHeight() / BMP_ROWS; } private void Update() { if (x >= gameView.getWidth() - width - speedx|| x + speedx <= 0) { } if (y >= gameView.getHeight() - height - speedy || y + speedy <= 0) { } currentFrame = ++currentFrame % BMP_COL; } public boolean onTouchEvent(MotionEvent event) { int index = event.getActionIndex(); int action = event.getActionMasked(); int pointerId = event.getPointerId(index); synchronized (gameView.getHolder()) { if (this.isCollition(event.getX(), event.getY())) { Log.d("", "Sprite touched!"); } switch (action) { case MotionEvent.ACTION_DOWN: if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } else { mVelocityTracker.clear(); } xTouch = event.getX(); yTouch = event.getY(); Log.d("", "You clicked: " + Float.toString(xTouch) + "," + Float.toString(yTouch)); mVelocityTracker.addMovement(event); break; case MotionEvent.ACTION_MOVE: mVelocityTracker.addMovement(event); mVelocityTracker.computeCurrentVelocity(1000); Log.d("", "X velocity: " + VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId)); Log.d("", "Y velocity: " + VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId)); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mVelocityTracker.recycle(); break; } } boolean ret = super.onTouchEvent(event); return ret; } public void onDraw(Canvas canvas) { MotionEvent event = null; Update(); int srcX = 1 * width; int srcY = 0 * height; Rect src = new Rect(srcX, srcY, srcX + width, srcY + height); Rect dst = new Rect(x, y, x + width, y + height); canvas.drawBitmap(bmp, src, dst, null); } public boolean isCollition(float x2, float y2) { return x2 > x && x2 < x + width && y2 > y && y2 < y + height; } } 类时,它可以完美地运行:

onTouchEvent

1 个答案:

答案 0 :(得分:1)

再次检查你的游戏逻辑并手动处理返回值,而不是 super.onTouchEvent(event);