I am new to android first of all so think of any newbie mistakes first
I am trying to add a fling function in my code.
public class MainGamePanel extends SurfaceView implements
SurfaceHolder.Callback, OnGestureListener {
private MainThread thread;
private Droid droid;
private Droid droid2;
private static final String TAG = gameView.class.getSimpleName();
private GestureDetector gestureScanner;
public MainGamePanel(Context context){
super(context);
getHolder().addCallback(this);
droid = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.playerbox2), 270, 300);
droid2 = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.playerbox2), 50, 300);
thread = new MainThread(getHolder(), this);
setFocusable(true);
gestureScanner = new GestureDetector(this);
}
public boolean onTouchEvent(MotionEvent event){
return gestureScanner.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.BLACK);
droid.draw(canvas);
droid2.draw(canvas);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
droid.setX(50);
droid.setY(50);
Log.d(TAG, "Coords: x=" + e1.getX() + ",y=" + e2.getY());
return true;
}
@Override
public boolean onDown(MotionEvent e) {
droid2.setX((int)e.getX());
droid2.setY((int)e.getY());
Log.d(TAG, "Coords: x=" + e.getX() + ",y=" + e.getY());
return false;
}
I got the gestureListener to work with: onDown, onLongPress, and onShowPress.
But i can't get any response with onFling, onSingleTapUp, and onScroll.
What mistake am I making? does it have to do with views?
I don't know what code would be useful to see.... so any suggestions would be much appreciated. Thank You!
onDown
is called before the onFling
event.
If you return false in onDown
method, the motion event propagation will stop.
Try to change return false
in return true
at the end of onDown
method.
The return values are explained, somewhat obtusely, in the Input Events page of the Dev guide. (scroll down below the example code to see).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With