• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

detecting swiping direction

moving player right or left depending on swiping

  • true

    Votes: 0 0.0%
  • false

    Votes: 0 0.0%

  • Total voters
    0

osos2323

Lurker
Hi;
In my Player.java class, I want to incorporate a function that moves my player according to the left or right swipes. I also used time in seconds in order to vary the movement in an cyclic way:






class MyGestureDetector extends SimpleOnGestureListener {
@override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-4:00"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("KK:mm");
date.setTimeZone(TimeZone.getTimeZone("GMT-4:00"));
String localTime = date.format(currentLocalTime);
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
X-=K;
int currentSeconds = cal.get(Calendar.SECOND);
Y=Y*Math.cos(18.currentSeconds);
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
X+=K;
int currentSeconds = cal.get(Calendar.SECOND);
Y=Y*Math.cos(18.currentSeconds);
}
} catch (Exception e) {
// nothing
}
return false;
}
'''






After some research, this is the class I included in player.java. So my question is: Is there a more efficient way to detect swiping directions(without creating any additional class) and what is your opinion about the code structure knowing that the objective is to change X according to swipe direction.

Thank you.
 
Back
Top Bottom