0

Here is a part of my code. I don't understand why MotionEvent.ACTION_POINTER_DOWN (a second finger touching the screen) isn't being detected. Can someone explain why this isn't working?

Single player works fine. Two player (With multi-touch) isn't.

What is working for two player -When the user presses only one of either halfs of the screen, it calls the goUp()

What doesn't work for two player -When the user presses one half of the screen, then presses the other half, the other half doesn't call goUp()

Also, is there a way to simulate multiple touches at once on an emulator so i can see the logs in the logcat?

private int pointerId = -1, pointerId2 = -1;

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

    // Depending on the touch, depends on the direction of the ricer (up or down)
    if(!TWO_PLAYER_MODE){
        switch (event.getActionMasked()) {

        // When finger touches the screen, ricer will accelerate up
        case MotionEvent.ACTION_DOWN:
            ricerView.goUp();
            break;

            // When finger is lifted from the screen, ricer will accelerate down
        case MotionEvent.ACTION_UP:
            ricerView.goDown();
        }
    }
    else{

        float touchY = event.getY();
        int pointerIndex = event.getActionIndex();
        int pointerID = event.getPointerId(pointerIndex);

        switch (event.getActionMasked()) {
        // When finger touches the screen, ricer will accelerate up
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            if( touchOnTopOfScreen(touchY) ){
                pointerId = pointerID;
                ricerView.goUp();
                log("RV1 down Id=" + pointerID + " Y-pos=" + touchY);
            }
            else{
                pointerId2 = pointerID;
                ricerView2.goUp();
                log("RV2 down Id=" + pointerID + " Y-pos=" + touchY);
            }
            break;

            // When finger is lifted from the screen, ricer will accelerate down
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            if( pointerId == pointerID ){
                ricerView.goDown();
                pointerId = -1;
                log("RV1 up Id=" + pointerID + " Y-pos=" + touchY);
            }
            if( pointerId2 == pointerID ){
                ricerView2.goDown();
                pointerId2 = -1;
                log("RV2 up Id=" + pointerID + " Y-pos=" + touchY);
            }
        }
    }

    // Delegate the touch to the gestureDetector 
    mGestureDetector.onTouchEvent(event);

    return true;

}   // End of onTouchEvent

private boolean touchOnTopOfScreen(float y){
    if(mDisplayHeight/2 > y)
        return true;
    else
        return false;
}
James
  • 4,573
  • 29
  • 32

2 Answers2

2

The problem is with the way you did your cases.

    case MotionEvent.ACTION_DOWN: 
    case MotionEvent.ACTION_POINTER_DOWN:
        if( touchOnTopOfScreen(touchY) ){
            pointerId = pointerID;
            ricerView.goUp();
            log("RV1 down Id=" + pointerID + " Y-pos=" + touchY);
        }
        else{//<---should be a separate if-statement
            pointerId2 = pointerID;
            ricerView2.goUp();
            log("RV2 down Id=" + pointerID + " Y-pos=" + touchY);
        }
        break;

In the above, there is an action for the extra pointer entering the screen and the first pointer but they are in a single if-statement. This means that if one condition is happening, the other cannot. You should split up the two players' movements into separate if-statements.

ylun.ca
  • 2,504
  • 7
  • 26
  • 47
  • Thank you for the respomse, but this doesn't work after testing it by changing the else to if(!touchOnTopOfScreen). It still works the same way. – James Mar 18 '14 at 19:00
  • In the goUp(), it simply changes a Boolean that is used in a scheduled thread. – James Mar 18 '14 at 19:05
  • The `touchOnTopOfScreen` returns a true and a false, correct? – ylun.ca Mar 18 '14 at 20:38
  • Yes, touchOnTopOfScreen returns a Boolean. – James Mar 18 '14 at 21:28
  • Okay, I am actually attempting to find a solution currently because as far as I can tell, what I said should have fixed the issue.. I have previously looked at this link, check it out: [Android touch](http://www.vogella.com/tutorials/AndroidTouch/article.html) – ylun.ca Mar 18 '14 at 21:31
  • I'm still looking for a solution too, and have been for days now. Yes, I've looked at that page, and I believe my code should work the way it is. – James Mar 18 '14 at 21:52
1

After giving up on this for a few weeks, i came back to this and found my solution which was actually a very easy fix. It all works now! It seems that when you try to get the x or y value of the event, you need to use event.getY(pointerIndex) rather than event.getY() The only change i made was:

int pointerIndex = event.getActionIndex();
int pointerID = event.getPointerId(pointerIndex);
float touchY = event.getY(pointerIndex);
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
James
  • 4,573
  • 29
  • 32