jmasterx
jmasterx

Reputation: 54143

Understanding Multitouch on Android?

I found this article: http://android-developers.blogspot.ca/2010/06/making-sense-of-multitouch.html which helped my understanding but I'm still not sure how to do what I'm trying to do.

In my game, I have a virtual analog stick and some buttons. Only 2 fingers will ever register at once. This is what I want. One for the analog stick and one for a button.

The main thing I'm unsure of is, say I put a finger down on the analog stick and move it around, then put a finger on the button, then release the button, the analog stick should keep moving to my first finger.

And vice versa, if the button touches first and then the analog stick, if I let go of the analog stick the button should still be pushed down.

Do touch pointers work in this fashion on Android, as in, once I put my finger down, regardless of any other fingers I put up or down, it will remember my first finger in order and give it a down, move move move up events?

Thanks

Ideally I wish I had a function like this:

void onTouch(int fingerID, int action, int x, int y)
{

}

Where each finger that is put down will receive down, move and up event when that finger goes up.

The game is a racing game so they need to be able to steer and push gas at the same time.

My problem is similar to this identified multi touch pointer in action_move

Edit:

I have this code:

private void onTouch(int finger, int action, float x, float y)
    {
        if(action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN)
        {
            createInput(finger, x, y);
        }
        else if(action == MotionEvent.ACTION_MOVE)
        {
            inputMove(finger, x, y);
        }
        else if(action == MotionEvent.ACTION_UP  || action == MotionEvent.ACTION_POINTER_UP)
        {
            destroyInput(finger, x, y);
        }
    }

    public void onTouch(MotionEvent ev)
     {
         final int pointerCount = ev.getPointerCount();
         for (int p = 0; p < pointerCount; p++) {
             onTouch(ev.getPointerId(p), ev.getAction(), ev.getX(p), ev.getY(p));
         }
     }

But it only works for the first one.

Upvotes: 2

Views: 219

Answers (2)

Dan D.
Dan D.

Reputation: 32391

You could take a look at the way the pinch is handled in AChartEngine for zoom. See this code, starting at line 80. It may look complex at the beginning, but it may help you on handling more use cases.

Upvotes: 0

Greg Giacovelli
Greg Giacovelli

Reputation: 10184

So from the examples given right in the MotionEvent class:

 public boolean onTouch(MotionEvent ev) {
     final int pointerCount = ev.getPointerCount();
     for (int p = 0; p < pointerCount; p++) {
         onTouch(ev.getPointerId(p), ev.getAction(), ev.getX(p), ev.getY(p));
     }
     return true;
 }

Upvotes: 1

Related Questions