views:

303

answers:

2

I'm writing an Android game that needs to receive touch events. My problem is that, whenever the user drags their finger along the screen, so many touch events get sent to the touch event handler (which I think runs as a separate thread) that my frame rate plummets! What's the best way I can limit the number of touch events that are handled per second?

For example, if my game runs at 60 fps, I really shouldn't need more than 1 touch event being handled every second. Can I do this is a way that doesn't lose any information (i.e. important information about where on the screen the user touched last)?

+2  A: 

My guess is that it's not the touch events, but the handling of them. Make sure you are just handling the type of events you need (i.e. down or move) and ignoring the others. Also you might want to store a time and return out of the event early if it's not 1 second greater than that stored time.

public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
sadboy
A: 

See Handling Events on this page. Even if you are not using GLSurfaceView it still might be applicable.

I think you should be able to handle at least 50 touchEvents per second, without a drop in framerate, at least on a phone with graphics acceleration (don't test it on the emulator);

What you don't want to do is block the rendering thread for any reason.

Nathan