7

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

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99

3 Answers3

2

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;
}
Community
  • 1
  • 1
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
0

MotionEvent has all information about touches that you need. You can get number of touches by executing event.getPointersCount(), and try to check MotionEvent.ACTION_POINTER_2_DOWN instead of MotionEvent.ACTION_DOWN. To get coordinates of each touch you can use event.getX(0) and event.getX(1), same is for y. If you have a case of MotionEvent.ACTION_MOVE with 2 touches, you will receive all this information in your motion event.

Ilya Vorobiev
  • 836
  • 8
  • 16
  • ACTION_POINTER_2_DOWN was deprecated in API level 8. Use ACTION_POINTER_INDEX_MASK to retrieve the data index associated with ACTION_POINTER_DOWN. I have tried this approach but is not working – Mauricio Gracia Gutierrez May 21 '15 at 19:26
0

Try below code. when multiple pointers touches on the screen,system generates the action events.we can keep track of individual pointers with in motion event using pointer id. Pointer id persists across touch events and also allows to track individual pointer across entire gesture.

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int index = event.getActionIndex();

            int pointerID = event.getPointerId(index);

            int action = event.getActionMasked();

            if(event.getPointerCount() > 1)
            {
                Log.i("TouchType ", "Multi Touch");
                for(int i = 0; i < event.getPointerCount(); i++)
                {
                    performAction(action);
                }
            }else
            {
                Log.i("TouchType ", "Single Touch");
                 performAction(action);
            }

        return true;
        }


  public void performAction(int action){
     switch(action)
    {
    case MotionEvent.ACTION_DOWN : 
        Log.i("OnTouch ", "Pressed");

         // Fire bullets 
    break;
    case MotionEvent.ACTION_MOVE :
        Log.i("OnTiouch", "move");
        //move character 
        break;
    case MotionEvent.ACTION_UP :
        Log.i("OnTiouch", "Up");
    break;
    default:
        Log.i("OnTiouch", "None");
    }
}