Search code examples
android2dandroid-canvasontouchlistenerontouch

Android onTouch() method doesn't work (swipe detection)


I'm programming Snake on Android with Android Studio 2.3.2

And in order to move the snake i've made an onTouchListener to detect if the user is swiping and in which direction (North, South, East, West)

In the following is the onTouch method of View.onTouchListener:

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            prevX = event.getX();
            prevY = event.getY();

            break;
        case MotionEvent.ACTION_UP:
            float newX = event.getX();
            float newY = event.getY();


            //Calculates where we swiped

            if (Math.abs(newX - prevX) > Math.abs(newY - prevY)) {
                //LEFT - RiGHT Direction

                if( newX > prevX) {
                    //RIGHT
                    gameEngine.updateDirection(Direction.East);
                } else {
                    //LEFT
                    gameEngine.updateDirection(Direction.West);
                }
            } else {
                // UP-DOWN Direction
                if (newY > prevY) {
                    //DOWN
                    gameEngine.updateDirection(Direction.South);
                } else {
                    //UP
                    gameEngine.updateDirection(Direction.North);
                }
            }

            break;
    }
    return false;
}

The problem is that it only detects LEFT and RIGHT (so EAST and WEST). And i don't know why UP and DOWN doesn't get detected.


Solution

  • Try the following code. It works for me.

    Java:

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class MainSnake extends AppCompatActivity implements View.OnTouchListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_snake);
            findViewById(R.id.main_view).setOnTouchListener(this);
        }
    
    
        float prevX, prevY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    prevX = event.getX();
                    prevY = event.getY();
    
                    return true;
                case MotionEvent.ACTION_UP:
                    float newX = event.getX();
                    float newY = event.getY();
    
    
                    //Calculates where we swiped
    
                    if (Math.abs(newX - prevX) > Math.abs(newY - prevY)) {
                        //LEFT - RiGHT Direction
    
                        if( newX > prevX) {
                            //RIGHT
                            Log.i("TOUCH INFO", "Right");
                        } else {
                            //LEFT
                            Log.i("TOUCH INFO", "Left");
                        }
                    } else {
                        // UP-DOWN Direction
                        if (newY > prevY) {
                            //DOWN
                            Log.i("TOUCH INFO", "Down");
                        } else {
                            //UP
                            Log.i("TOUCH INFO", "Up");
                        }
                    }
    
                    break;
            }
            return false;
        }
    }
    

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_view">
    
    </android.support.constraint.ConstraintLayout>