How to detect a TOUCH event with the other finger when ACTION_MOVE is in the process

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

In the following code, I can detect both ACTION_MOVE (in a specific area of ​​the screen) and 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 can’t move and shoot at the same time (I need to stop moving to shoot and vice versa)

I know that Android can handle events with multiple touches , but it doesn't matter how to use this to handle these events and at the same time so that the player can move and shoot at the same time

I also try to use getActionMasked with no luck

+8
android event-handling
source share
3 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; } 
+2
source share

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

0
source share

Try entering the code below. when several pointers touch the screen, the system generates action events. We can track individual pointers using a motion event using a pointer identifier. The identifier of the pointer is stored in touch events, and also allows you to track an individual pointer in the whole 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"); } } 
0
source share

All Articles