Fling手势没有执行

时间:2014-11-23 04:30:15

标签: android motionevent

我正在尝试在游戏中实施fling guest。但是,似乎没有检测到投掷手势(我正在使用Android模拟器)。

我不确定是什么问题,因为我已经调试了它并且没有检测到手势。

这是我的代码,我道歉,因为它有点乱。

    public class PlayActivity extends Activity {

        private static final String TAG = "PlayActivity";

        Maze maze;
        GameView gameView;

        ViewConfiguration vc;
        int swipeMinDistance;
        int swipeThresholdVelocity;
        int swipeMaxOffPath;
        private GestureDetector gestureDetector;
        View.OnTouchListener gestureListener;

        /**
         * Sets up UI 
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            gameView = new GameView(this);
            gameView.setKeepScreenOn(true);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                       WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(gameView);
            this.maze = MazeContainer.getMaze();
            ViewConfiguration vc = ViewConfiguration.get(getApplicationContext());
            swipeMinDistance = vc.getScaledPagingTouchSlop();
            swipeThresholdVelocity = vc.getScaledMinimumFlingVelocity();
            swipeMaxOffPath = vc.getScaledTouchSlop();
            gestureDetector = new GestureDetector(this, new MyGestureDetector());
            gestureListener = new View.OnTouchListener() { //I don't know if I set this part up correctly
                    public boolean onTouch(View v, MotionEvent event) {
                        return gestureDetector.onTouchEvent(event);
                    }
                };
            Toast.makeText(this, "Play Activity", Toast.LENGTH_LONG).show();
        }

        /**
         * Makes sure that once back button is pressed, Title activity will start
         */
        @Override
        public void onBackPressed() {
            Log.v(TAG, "Back button pressed. Going back to AMazeActivity.");
            Intent intent = new Intent(getApplicationContext(), AMazeActivity.class);
            startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
            finish();
        }

        class MyGestureDetector extends SimpleOnGestureListener {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //This method never gets executed
                try {
                    if (Math.abs(e1.getY() - e2.getY()) > swipeMaxOffPath)
                        return false;
                    // right to left swipe
                    if(e1.getX() - e2.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) {
    //                    Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                        maze.keyDown(null, 'h');
                    }  else if (e2.getX() - e1.getX() > swipeMinDistance && Math.abs(velocityX) > swipeThresholdVelocity) {
    //                    Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                        maze.keyDown(null, 'j');
                    }
                } catch (Exception e) {
                    // nothing
                }
                return false;
            }

                @Override
            public boolean onDown(MotionEvent e) {
                  return true;
            }
        }
 {

可疑区域可能正在实例化gesturesListener。但由于调试器没有告诉我任何事情,因为没有任何问题,我不确定。我所知道的是,在模拟器上拖动和加速鼠标不会在MyGestureDetector内执行适当的方法。

感谢任何提示!

1 个答案:

答案 0 :(得分:2)

您遗失的一件事是您没有将活动的触摸事件发送给您的听众。假设您要检测GameView对象上的手势,构建侦听器后所需的一行是:

gameView.setOnTouchListener(gestureListener);