多点触控的Andr​​oid移动不能在同一时间两个指针 [英] android multitouch cant move 2 pointers at same time

查看:137
本文介绍了多点触控的Andr​​oid移动不能在同一时间两个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在游戏中一个奇怪的问题。我使用的是2操纵杆,一个用于拍摄/瞄准和一个用于移动我的性格。出于某种原因,我的多点触控方法一次只能注册一个动作。第二个指针被注册的时候我preSS下来,但我的 ACTION_MOVE 只适用于第一个指针。这是奇怪的硫化铜,这意味着它确实需要一个以上的指针,但它的不能同时移动一个以上的指针的。香港专业教育学院问这个的gamedev.stackexchange和活跃了一周左右,接到了几个答案,但没有什么使得它的工作100%。我也试过在我自己的时间。

I have a weird problem in my game. I'm using 2 joysticks, one for shooting/aiming and one for moving my character. For some reason my multitouch method only registers one movement at a time. The second pointer gets registered when I press down, but my ACTION_MOVE only works for the first pointer. This is weird cus this means it does take more then one pointer, but it cant move more then one pointer at the same time. Ive asked this on gamedev.stackexchange and its been active for about a week, gotten a couple of answer but nothing that makes it work 100%. And I've tried for hours on my own.

code为onTouch法:

Code for onTouch-method:

    //global variables
    private int movePointerId = -1;
    private int shootingPointerId = -1;

public void update(MotionEvent event) {

    if (event == null && lastEvent == null) {
        return;
    } else if (event == null && lastEvent != null) {
        event = lastEvent;
    } else {
        lastEvent = event;
    }   

    // grab the pointer id 
    int action = event.getAction();
    int actionCode = action & MotionEvent.ACTION_MASK;
    int actionIndex = event.getActionIndex();
    int pid = action >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    int x = (int) event.getX(pid);
    int y = (int) event.getY(pid); 
    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;
                        movePointerId = pid;
                        dragging = true;
                        //checks if Im pressing the joystick used for moving
                    }
                 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;
                        shootingPointerId = pid;
                        shooting=true;
                        //checks if Im pressing the joystick used for shooting
                    }
                }catch(Exception e){

                }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_OUTSIDE:

           if( pid == movePointerId ){
              movePointerId = -1;
              dragging = false;
              }
           else if( pid == shootingPointerId ){
              shootingPointerId = -1;
              shooting=false;
              }
            actionString = "UP";
            break;  
        case MotionEvent.ACTION_MOVE: // this is where my problem is

          if( pid == movePointerId ) {
                        movingPoint.x = x;
                        movingPoint.y = y;
          } else if( pid == shootingPointerId ) {
                        shootingPoint.x = x;
                        shootingPoint.y = y;
          }
                actionString = "MOVE";
                break;

    }

如果我打印 actionString PID 这表明移动时,它仅检查 PID = 0 ,但是当我preSS下降( ACTION_POINTER_DOWN )我可以看到,它并注册其他 PID ,这是真的什么困惑我。

If I print actionString and pid it shows that when moving, it only checks pid=0, but when i press down ( ACTION_POINTER_DOWN ) I can see that it does register another pid, this is whats really confusing me.

只是为了更清楚,当我倒在preSS第二个指针,例如我的拍摄棒,它需要在那里我pressed的位置,即使我移动另一个操纵杆在同样的时间,但它保持在那里,直到我放开其他操纵杆。 Furhter证明,它并注册多于1触摸和更多的则1 PID

Just to make it more clear, when I press the second pointer down on for example my shooting-stick, it takes the position of where I pressed, even if I'm moving the other joystick at the same time, but it stays there until I let go of the other joystick. Furhter proof that it does register more then 1 touch and more then 1 pid.

请让我知道如果您需要任何进一步explenation。

Please let me know if you need any further explenation.

推荐答案

我做了一些改动到code,我认为应该解决这个问题。阿尔至少它工作正常,我...

I've made a couple of changes to your code, that I believe should solve the problem. Al least it works fine for me ...

    //global variables 
private int movePointerId = -1; 
private int shootingPointerId = -1; 

public void update(MotionEvent event) { 

if (event == null && lastEvent == null) { 
    return; 
} else if (event == null && lastEvent != null) { 
    event = lastEvent; 
} else { 
    lastEvent = event; 
}    

// grab the pointer id  
int action = event.getAction(); 
int actionCode = action & MotionEvent.ACTION_MASK; 
int pid = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int fingerid = event.getPointerId(pid);

//int actionIndex = event.getActionIndex(); 
//int pid = action >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; 
int x = (int) event.getX(pid); 
int y = (int) event.getY(pid);  
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; 
                    //movePointerId = pid; 
                    movePointerId = fingerid; 
                    dragging = true; 
                    //checks if Im pressing the joystick used for moving 
                } 
             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; 
                    //shootingPointerId = pid; 
                    shootingPointerId = fingerid; 
                    shooting=true; 
                    //checks if Im pressing the joystick used for shooting 
                } 
            }catch(Exception e){ 

            } 
        break; 
    case MotionEvent.ACTION_UP: 
    case MotionEvent.ACTION_POINTER_UP: 
    case MotionEvent.ACTION_CANCEL: 
    case MotionEvent.ACTION_OUTSIDE: 

       if( fingerid == movePointerId ){  //changed this line
          movePointerId = -1; 
          dragging = false; 
          } 
       else if( fingerid == shootingPointerId ){  //changed this line
          shootingPointerId = -1; 
          shooting=false; 
          } 
        actionString = "UP"; 
        break;   
    case MotionEvent.ACTION_MOVE: // this is where my problem is 

      if( fingerid == movePointerId ) { //changed this line
                    movingPoint.x = x; 
                    movingPoint.y = y; 
      } else if( fingerid == shootingPointerId ) { //changed this line
                    shootingPoint.x = x; 
                    shootingPoint.y = y; 
      } 
            actionString = "MOVE"; 
            break; 

} 

这样做的原因是,当你释放一个手指去指针ID某些设备可能会改变。例如,第一手指指针接收ID为1,那么美元,其中接收指示器ID 2个P $ PSS第二根手指,而且如果再松开手指1,手指从2指针标识可能会变得1.这听起来可能有点混乱,但你应避免与上述这个手指的ID的问题。

The reason for this is that on some devices de pointer id may change when you release one finger. For example, first finger receives pointer id 1, then you press second finger which receives pointer id 2, and if then you release finger 1, pointer id from finger 2 may become 1. It may sound a bit confusing, but you should avoid the issue with this finger id above.

好运。

这篇关于多点触控的Andr​​oid移动不能在同一时间两个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆