2

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.

Urbs
  • 131
  • 9
  • that has nothing to do with your onTouchListener(), your calculation doesn´t meet the requirements. `if (Math.abs(newX - prevX) > Math.abs(newY - prevY))` seems to be always true. Second scenario, `newY>prevY` then `newY` is always bigger.... – Opiatefuchs Jun 06 '17 at 12:57
  • @Opiatefuchs Okay the problem is I don't understand why it's always true because when i swipe along the y-axis it should have higher value then the x-axis. Because the difference when i swipe up/down on x-axis should be near zero. – Urbs Jun 06 '17 at 13:10
  • usually you have to compare just prevX and newX for x axis and prevY and newY. Why you are comparing x-axis against y-axis? – Opiatefuchs Jun 06 '17 at 13:23
  • @Opiatefuchs i'm comparing X and Y to look what changes more, because you'll never swipe 100% straight, so what changes more is the line you're swiping. – Urbs Jun 06 '17 at 13:41

2 Answers2

2

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>
  • Make sure your layout is named activity_main_snake or change it accordingly. – Jacob Lewis Jun 06 '17 at 14:37
  • It works i can see that he recognizes my moves but now i need to add the snakeView again to your Constraint Layout – Urbs Jun 06 '17 at 14:44
  • Tank you for you afford but you were right my version was right before, i found the error in the method "updateDirection()" I'm really sorry for that but it's first time programming in 2d and with handlers etc, it's very much for me right now ^^ – Urbs Jun 06 '17 at 14:54
  • Glad to help! Good Luck on the rest. – Jacob Lewis Jun 06 '17 at 15:32
0

You must return "True" after ACTION_DOWN. This tells Android to watch for the ACTION_UP event.

switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        prevX = event.getX();
        prevY = event.getY();

        return true;
  • it watched for the ACTION_UP event before (Left and Right directions are working) and even when i edit the code like you mentioned it changes nothing. – Urbs Jun 06 '17 at 13:39
  • I tested the code, and it works for me. So the logic is correct. Try testing for ACTION_CANCEL as well. Maybe the view is not large enough to detect every direction. – Jacob Lewis Jun 06 '17 at 13:46
  • I've added (case MotionEvent.ACTION_CANCEL: break;) at the end but it still doesn't work. I'm testing the app on a 720p Screen and there should be enough space. – Urbs Jun 06 '17 at 13:51
  • What type of view are you adding the event testing to? Make sure it is not conflicting with other views. Like I said above, I have a working sample of your code. So the comparison logic is sound. – Jacob Lewis Jun 06 '17 at 13:55
  • I've made an SnakeView which adds this onTouchListener and I don't got any other views. – Urbs Jun 06 '17 at 14:05
  • Edit: I also got the layout xml view but it's an empty activity – Urbs Jun 06 '17 at 14:13