Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a TOUCH event with another finger while an ACTION_MOVE is in process

I am developing a game and I need to be able to detect that one finger is performing a MOVE while posibly another finger can TOUCH another part of the screen.

With the following code I am able to detect both the ACTION_MOVE (on certain region of the screen) and the ACTION_DOWN

public boolean onTouch(View v, MotionEvent event) {
    final int dest_x = (int) event.getX();
    final int dest_y = (int) event.getY();

    onTrackPad = dbSettings.TRACK_PAD.contains(dest_x, dest_y);

    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            if (onTrackPad) 
            {
                //move character 
            }         
            break;
        case MotionEvent.ACTION_DOWN:
            // Fire bullets 
            break;
    }
    //The event was consumed
    return true;
}

The problem is that I am not able to move and fire at the same time (I need to stop moving in order to fire and viceversa)

I am aware that Android can handle multi-touch events but have not figure it how to use that to be able to process these events and the same time so that the player can move and fire at the same time

I have also try using the getActionMasked without any luck

like image 958
Mauricio Gracia Gutierrez Avatar asked May 18 '15 16:05

Mauricio Gracia Gutierrez


People also ask

What is the difference between touchmove and touchend events?

For the touchmove event, it is a list of the touch points that have changed since the last event. For the touchend event, it is a list of the touch points that have been removed from the surface (that is, the set of touch points corresponding to fingers no longer touching the surface).

How do the touch events interfaces support multi-touch interactions?

The touch events interfaces support application specific single and multi-touch interactions such as a two-finger gesture. A multi-touch interaction starts when a finger (or stylus) first touches the contact surface. Other fingers may subsequently touch the surface and optionally move across the touch surface.

How many times does the touchmove event trigger?

The touchmove event will be triggered once for each movement, and will continue to be triggered until the finger is released. Note: The touchmove event will only work on devices with a touch screen.

What is the use of touchaction event handler?

The TouchAction event handler processes all the touch events for all the BoxView elements, but it needs to exercise some caution: It can't allow two fingers on a single BoxView because the program only implements dragging, and the two fingers would interfere with each other.


1 Answers

After reading this question Android MotionEvent.getActionIndex() and MultiTouch

This is how I solved the problem

public boolean onTouch(View v, MotionEvent event) {
    int dest_x ;
    int dest_y ;

    p = event.getActionIndex() ;

    dest_x = (int) event.getX(p);
    dest_y = (int) event.getY(p);

    onTrackPad = dbSettings.TRACK_PAD.contains(dest_x, dest_y);

    action = event.getActionMasked() ;

    switch (action) {
        case MotionEvent.ACTION_MOVE:
            if (onTrackPad) 
            {
                //move character 
            }         
            break;
        case MotionEvent.ACTION_DOWN:
            // Fire bullets 
            break;
    }
    //The event was consumed
    return true;
}
like image 100
Mauricio Gracia Gutierrez Avatar answered Nov 14 '22 23:11

Mauricio Gracia Gutierrez