私は今、一晩中これを見つめています。Android 用のシンプルなインタラクティブ ボール ゲームを作成しようとしていますが、onFling メソッドが機能していないようです。onFling にあるログが LogCat に表示されず、フリング ジェスチャに応答がありません。私は、ジェスチャー検出器またはジェスチャーリスナーで何か間違ったことをしていると思います。やっと眠れるように助けてください :) . これは私のコードです:
これは onCreate の外にありますが、アクティビティ内にあります。
@Override
public boolean onTouchEvent(MotionEvent me) {
return gestureDetector.onTouchEvent(me);
}
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
ballSpeed.x = velocityX;
ballSpeed.y = velocityY;
Log.d("TAG", "----> velocityX: " + velocityX);
Log.d("TAG", "----> velocityY: " + velocityY);
return true;
}
};
GestureDetector gestureDetector = new GestureDetector(null,
simpleOnGestureListener);
編集:
どうやら上記のコードは動作します。つまり、間違いは別の場所にあるということです。@bobnoble は、onCreate() メソッドを投稿する必要があると言っています。
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); // hide title bar
getWindow().setFlags(0xFFFFFFFF,LayoutParams.FLAG_FULLSCREEN| LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create pointer to main screen
final FrameLayout mainView = (android.widget.FrameLayout) findViewById(R.id.main_view);
// get screen dimensions
Display display = getWindowManager().getDefaultDisplay();
try {
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
} catch (NoSuchMethodError e) {
screenWidth = display.getWidth();
screenHeight = display.getHeight();
}
ballPosition = new android.graphics.PointF();
ballSpeed = new android.graphics.PointF();
// create variables for ball position and speed
ballPosition.x = 4 * screenWidth / 5;
ballPosition.y = 2 * screenHeight / 3;
ballSpeed.x = 0;
ballSpeed.y = 0;
// create initial ball
ballView = new BallView(this, ballPosition.x, ballPosition.y, 20);
mainView.addView(ballView); // add ball to main screen
ballView.invalidate(); // call onDraw in BallView
//listener for touch event
mainView.setOnTouchListener(new android.view.View.OnTouchListener() {
public boolean onTouch(android.view.View v, android.view.MotionEvent e) {
//set ball position based on screen touch
ballPosition.x = e.getX();
ballPosition.y = e.getY();
ballSpeed.y = 0;
ballSpeed.x = 0;
//timer event will redraw ball
return true;
}});
}