Skip to main content
Removed the superfluous solved tag in the title
Link
Kylotan
  • 24.3k
  • 3
  • 52
  • 94

returning correct multiTouch id [SOLVED]

added 839 characters in body; edited title; Post Made Community Wiki
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55

returning correct multiTouch id [SOLVED]

this is how I will post my entire onTouch method here with some variable-changes to makesolved it more understandable. Since I do not know where Im going wrong.with your help! this is for anyone that might run into a similar problem:

    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); 
        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;
                            movePointerId = pid;
                            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;
                            shootingPointerId = pid;
                            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( pidid == movePointerId draggingId){
                  movePointerId = -1;
                  dragging = false;
                  }
               else if( pidid == shootingPointerId shootingId){
                  shootingPointerId = -1;
                  shooting=false;
                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 pid&& id == movePointerIddraggingId) {
                        if(xx > 0 && xx < (steeringxMesh + joystick.get_joystickBg().getWidth() * 2)
                            && yy > yMesh - (joystick.get_joystickBg().getHeight()) && yy < panel.getHeight()) {
                            movingPoint.x = x;xx;
                            movingPoint.y = y;yy;
                        }
                        else
                            dragging = false;
                        }
                    if(shooting pid&& id == shootingPointerIdshootingId){
                        if(xx > shootingxMesh - (joystick.get_joystickBg().getWidth()) && xx < panel.getWidth()
                            && yy > yMesh - (joystick.get_joystickBg().getHeight()) && yy < panel.getHeight()) {
                            shootingPoint.x = x;xx;
                            shootingPoint.y = y;yy;                            
                        }
                        else
                            shooting = false;
                        }
                    }

                    actionString = "MOVE";
                    break;

        }
    Log.d(TAG, "actionsString: " + actionString + ", pid: " + pid + ", x: " + x + ", y: " + y);
   

returning correct multiTouch id

I will post my entire onTouch method here with some variable-changes to make it more understandable. Since I do not know where Im going wrong.

    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;
                        }
                    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;
                        }
                    }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:
            
              if( pid == movePointerId ) {
                            movingPoint.x = x;
                            movingPoint.y = y;
              } else if( pid == shootingPointerId ) {
                            shootingPoint.x = x;
                            shootingPoint.y = y;
              }
                    actionString = "MOVE";
                    break;

        }
    Log.d(TAG, "actionsString: " + actionString);
   

returning correct multiTouch id [SOLVED]

this is how I solved it with your help! this is for anyone that might run into a similar problem:

    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);
   
added 150 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
   int pointerCountpublic =void event.getPointerCountupdate(MotionEvent event); {
    
 if (event == null for&& (intlastEvent i== =null) 0;{
 i < pointerCount; i++)
    {
return;
    } else if (event int== idnull && lastEvent != event.getPointerId(inull); {
        intevent x= lastEvent;
    } else {
        lastEvent = (int)event;
 event.getX(id);   }   

        int// ygrab =the (int)pointer event.getY(id); 
        int action = event.getAction();
        int actionCode = action & MotionEvent.ACTION_MASK;
        int actionIndex = event.getActionIndex();
        int pid = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;ACTION_POINTER_INDEX_SHIFT;
        Stringint actionString;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";
                break;try{
            case MotionEvent.ACTION_UP:
       if(x > 0 && x < steeringxMesh + (joystick.get_joystickBg().getWidth() * shooting=false;2)
                dragging=false;
            && y > yMesh actionString- =(joystick.get_joystickBg().getHeight()) "UP";
&& y < panel.getHeight()){
             break;  
             case MotionEventmovingPoint.ACTION_POINTER_DOWN:x = x;
                actionString = "PNTR DOWN";         movingPoint.y = y;
                break;
            casemovePointerId MotionEvent.ACTION_POINTER_UP:= pid;
                shooting=false;
            dragging = true;
  dragging=false;
                actionString = "PNTR UP";   }
                break;
    else if(x > shootingxMesh - (joystick.get_joystickBg().getWidth()) && x case< MotionEventpanel.ACTION_CANCEL:getWidth()
                shooting=false;
            && y > yMesh dragging=false;- (joystick.get_joystickBg().getHeight()) && y < panel.getHeight()){
                actionString            shootingPoint.x = "CANCEL";x;
                break;
            case MotionEventshootingPoint.ACTION_MOVE:
y = y;
              
               shootingPointerId = try{pid;
                    if(x > 0 && x < steeringxMesh + (joystick.get_joystickBg().getWidth()shooting=true;
 * 2)
                      }
      && y > yMesh - (joystick.get_joystickBg().getHeight()) && y < panel.getHeight     }catch()Exception e){ 

                    }
        movingPoint.x = x;
      break;
            case MotionEvent.ACTION_UP:
          movingPoint.y = y;case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
  dragging = true;
         
               if( }pid == movePointerId ){
                  movePointerId = else-1;
 if(x > shootingxMesh - (joystick.get_joystickBg().getWidth()) && x < panel.getWidth()         dragging = false;
                  }
          && y > yMesh - (joystick.get_joystickBg().getHeightelse if()) &&pid y== <shootingPointerId panel.getHeight()){
                  shootingPointerId = -1;
        shootingPoint.x = x;        shooting=false;
                  }
          shootingPoint.y      actionString = y;"UP";
                break;   
          shooting=true;  case MotionEvent.ACTION_MOVE:
             
             }else if( pid == movePointerId ) {
                            shooting=false;movingPoint.x = x;
                            dragging=false;movingPoint.y = y;
              } else if( pid == shootingPointerId ) {
   }
                    }catch(Exception e){    shootingPoint.x = x;
                        
     shootingPoint.y = y;
              }
                    actionString = "MOVE";
                    break;

        }
    Log.d(TAG, "actionsString: " + actionString);
   

adding code for how I move the sprite using this:

   angle = Math.atan2(movingPoint.y - initY,
                 movingPoint.x - initX) / (Math.PI / 180);

        //my sprites position
        spritePosition.x += Math.cos(angle * (Math.PI / 180)) //add delta
                * speed;
        spritePosition.y += Math.sin(angle * (Math.PI / 180)) //add delta
                * speed;

if I print spritePosition its value dosnt change when I press both joysticks, even tho you can very obviously see that the characters speed changes. Rly weird.

   int pointerCount = event.getPointerCount();
    
     for (int i = 0; i < pointerCount; i++)
    {

        int id = event.getPointerId(i);
        int x = (int) event.getX(id);
        int y = (int) event.getY(id); 
        int action = event.getAction();
        int actionCode = action & MotionEvent.ACTION_MASK;
        int actionIndex = event.getActionIndex();
        int pid = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        String actionString;
        
        
        switch (actionCode)
        {
            case MotionEvent.ACTION_DOWN:
                actionString = "DOWN";
                break;
            case MotionEvent.ACTION_UP:
                shooting=false;
                dragging=false;
                actionString = "UP";
                break;  
             case MotionEvent.ACTION_POINTER_DOWN:
                actionString = "PNTR DOWN";
                break;
            case MotionEvent.ACTION_POINTER_UP:
                shooting=false;
                dragging=false;
                actionString = "PNTR UP";
                break;
            case MotionEvent.ACTION_CANCEL:
                shooting=false;
                dragging=false;
                actionString = "CANCEL";
                break;
            case MotionEvent.ACTION_MOVE:
                
                 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;
                        }
                    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;
                        }else{
                            shooting=false;
                            dragging=false;
                        }
                    }catch(Exception e){
                        
                     }
                    actionString = "MOVE";

        }

adding code for how I move the sprite using this:

   angle = Math.atan2(movingPoint.y - initY,
                 movingPoint.x - initX) / (Math.PI / 180);

        //my sprites position
        spritePosition.x += Math.cos(angle * (Math.PI / 180)) //add delta
                * speed;
        spritePosition.y += Math.sin(angle * (Math.PI / 180)) //add delta
                * speed;

if I print spritePosition its value dosnt change when I press both joysticks, even tho you can very obviously see that the characters speed changes. Rly weird.

    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;
                        }
                    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;
                        }
                    }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:
             
              if( pid == movePointerId ) {
                            movingPoint.x = x;
                            movingPoint.y = y;
              } else if( pid == shootingPointerId ) {
                            shootingPoint.x = x;
                            shootingPoint.y = y;
              }
                    actionString = "MOVE";
                    break;

        }
    Log.d(TAG, "actionsString: " + actionString);
   
Notice removed Draw attention by Green_qaue
Bounty Ended with ADB's answer chosen by Green_qaue
deleted 32 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
added 384 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
added 384 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
deleted 188 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
deleted 23 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
added 88 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
deleted 78 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
added 4 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
Notice added Draw attention by Green_qaue
Bounty Started worth 50 reputation by Green_qaue
edited title
Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
Tweeted twitter.com/#!/StackGameDev/status/248119459414433794
added 143 characters in body
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading
Source Link
Green_qaue
  • 1.9k
  • 4
  • 25
  • 55
Loading