He pasado innumerables horas leyendo tutoriales y examinando todas las preguntas relacionadas con multiTouch desde aquí y Stackoverflow. Pero no puedo entender cómo hacer esto correctamente. Utilizo un bucle para obtener mi pointerId, no veo mucha gente haciendo esto, pero es la única forma en que he logrado que funcione.
Tengo dos joysticks en mi pantalla, uno para moverme y otro para controlar la rotación de mis sprites y el ángulo en el que dispara, como en Monster Shooter. Ambos funcionan bien.
Mi problema es que cuando muevo mi sprite al mismo tiempo que estoy disparando, mi touchingPointmovimiento se establece en el touchingPointde mi disparo, ya que xy yes más alto en touchingPointmi disparo ( moving-sticken el lado izquierdo de la pantalla, shooting-sticken el lado derecho) , mi sprite se acelera, esto crea un cambio no deseado en la velocidad de mi sprite.
¡Así es como lo resolví con tu ayuda! Esto es para cualquier persona que pueda tener un problema similar:
public void update(MotionEvent event) {
if (event == null && lastEvent == null) {
return;
} else if (event == null && lastEvent != null) {
event = lastEvent;
} else {
lastEvent = event;
}
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
int pid = action >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int x = (int) event.getX(pid);
int y = (int) event.getY(pid);
int index = event.getActionIndex();
int id = event.getPointerId(index);
String actionString = null;
switch (actionCode)
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
actionString = "DOWN";
try{
if(x > 0 && x < steeringxMesh + (joystick.get_joystickBg().getWidth() * 2)
&& y > yMesh - (joystick.get_joystickBg().getHeight()) && y < panel.getHeight()){
movingPoint.x = x;
movingPoint.y = y;
dragging = true;
draggingId = id;
}
else if(x > shootingxMesh - (joystick.get_joystickBg().getWidth()) && x < panel.getWidth()
&& y > yMesh - (joystick.get_joystickBg().getHeight()) && y < panel.getHeight()){
shootingPoint.x = x;
shootingPoint.y = y;
shooting=true;
shootingId=id;
}
}catch(Exception e){
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
if(id == draggingId)
dragging = false;
if(id == shootingId)
shooting = false;
actionString = "UP";
break;
case MotionEvent.ACTION_MOVE:
for(index=0; index<event.getPointerCount(); index++) {
id=event.getPointerId(index);
int xx = (int) event.getX(index); //pro naming of variable
int yy = (int) event.getY(index);
if(dragging && id == draggingId) {
if(xx > 0 && xx < (steeringxMesh + joystick.get_joystickBg().getWidth() * 2)
&& yy > yMesh - (joystick.get_joystickBg().getHeight()) && yy < panel.getHeight()) {
movingPoint.x = xx;
movingPoint.y = yy;
}
else
dragging = false;
}
if(shooting && id == shootingId){
if(xx > shootingxMesh - (joystick.get_joystickBg().getWidth()) && xx < panel.getWidth()
&& yy > yMesh - (joystick.get_joystickBg().getHeight()) && yy < panel.getHeight()) {
shootingPoint.x = xx;
shootingPoint.y = yy;
}
else
shooting = false;
}
}
actionString = "MOVE";
break;
}
Log.d(TAG, "actionsString: " + actionString + ", pid: " + pid + ", x: " + x + ", y: " + y);
No publicaría tanto código si no tuviera una pérdida absoluta de lo que estoy haciendo mal. Simplemente no puedo entender bien cómo funciona el MultiTouching.
básicamente movingPoint cambia para mi primer y segundo dedo. Lo ato a un cuadro, pero mientras sostengo un dedo dentro de este cuadro, cambia su valor en función de donde toca mi segundo dedo. Se mueve en la dirección correcta y nada da un error, el problema es el cambio de velocidad, es casi como si sumara los dos puntos de contacto.