How to Implement Swipe In Android Game Without View How to Implement Swipe In Android Game Without View android android

How to Implement Swipe In Android Game Without View


You can do this in two ways:

Option 1:

Within your game's underlying activity class:

class MyGestureDetector extends SimpleOnGestureListener {        @Override        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                float velocityY) {            try {                float slope = (e1.getY() - e2.getY()) / (e1.getX() - e2.getX());                float angle = (float) Math.atan(slope);                float angleInDegree = (float) Math.toDegrees(angle);                // left to right                if (e1.getX() - e2.getX() > 20 && Math.abs(velocityX) > 20) {                    if ((angleInDegree < 45 && angleInDegree > -45)) {                                //code for left to right swipe should go here        }                    // right to left fling                } else if (e2.getX() - e1.getX() > 20                        && Math.abs(velocityX) > 20) {                    if ((angleInDegree < 45 && angleInDegree > -45)) {      //code for right to left swipe should go here                    }                }                return true;            } catch (Exception e) {                // nothing            }            return false;        }    }

You can then register any view to receive/listen for the gestures:

 final GestureDetector  gestureDetector = new GestureDetector(new MyGestureDetector());                gameView.setOnTouchListener(new View.OnTouchListener() {                    @Override                    public boolean onTouch(View v, MotionEvent event) {                        if (gestureDetector.onTouchEvent(event)) return false;                        return false;                    }                });         //the parent layout                   findViewById(R.id.parent_layout).setOnTouchListener(new View.OnTouchListener() {                    @Override                    public boolean onTouch(View v, MotionEvent event) {                        if (gestureDetector.onTouchEvent(event)) return false;                        return false;                    }                });         //an image view        findViewById(R.id.image_view).setOnTouchListener(new View.OnTouchListener() {                    @Override                    public boolean onTouch(View v, MotionEvent event) {                        if (gestureDetector.onTouchEvent(event)) return false;                        return false;                    }                });

Option 2:

If the GameScreen class directly has access to touch events, you can read them and implement a logic for swipe actions. The code for that is similar to Mostafa Gazar's answer.


You should post your GameScreen as Nana Ghartey

Assuming you have access to touch data you can do something like the following

private static final int DEFAULT_THRESHOLD = 36;// Better to be something in dps.... = new View.OnTouchListener() {    int initialX = 0;    int initialY = 0;    final float slop = ViewConfiguration.get(context).getScaledTouchSlop();    public boolean onTouch(final View view, MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            initialX = (int) event.getX();            initialY = (int) event.getY();        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {            int currentX = (int) event.getX();            int currentY = (int) event.getY();            int offsetX = currentX - initialX;            int offsetY = currentY - initialY;            if (Math.abs(offsetX) > slop) {                if (offsetX > DEFAULT_THRESHOLD) {                    // TODO :: Do Right to Left action!                } else if (offsetX < -DEFAULT_THRESHOLD) {                    // TODO :: Do Left to Right action!                }            }            if (Math.abs(offsetY) > slop) {                if (offsetY > DEFAULT_THRESHOLD) {                    // TODO :: Do Bottom to Top action!                } else if (offsetY < -DEFAULT_THRESHOLD) {                    // TODO :: Do Top to Bottom action!                }            }        } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {            // Do nothing!        }};

Good thing about this implementation is that user will not have to take her hand off the screen while controlling snake direction.


Put the super methods in your code, example:

public void onSwipeTop() {    super.onSwipeTop();    Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();}