Question

I am writing an app that has two soft d-pads on the screen. I am having extreme difficulty trying to capture and understand all the touch events for two simultaneous touches because the fact that every ACTION_MOVE event says that the id is equal to 0.

What I have are two 'touch' objects that get piped into the collision detection system when there is a touch. Remember I have two d-pads and both need to have the ability to control at the same time.

Does anyone have any suggestions. Here is my onTouchEvent method. Keep in mind it may be difficult to understand the context because I use a cusomt 'touch' gameobject for collision detection with the d-pads.

@Override
public void onTouchEvent(MotionEvent e)
{   
    int index = e.getActionIndex();


    int action = MotionEventCompat.getActionMasked(e);
    // Get the index of the pointer associated with the action.
    //index = MotionEventCompat.getActionIndex(e);

    int id = e.getPointerId(index);


    if(touch1.pointerID<0)
        touch1.pointerID = id;
    else
        touch2.pointerID = id;

    switch(action)
    {
        case MotionEvent.ACTION_DOWN:
        {
            if(id == touch1.pointerID)
            {
                touch1.setCollideable(true);
                touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();
            }
            else if(id==touch2.pointerID)
            {
                touch2.setCollideable(true);
                touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();

            }

            break;  
        }
        case MotionEvent.ACTION_POINTER_DOWN:
        {
            if(id == touch1.pointerID)
            {
                touch1.setCollideable(true);
                touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();
            }
            else if(id==touch2.pointerID)
            {
                touch2.setCollideable(true);
                touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();

            }
            break;
        }
        case MotionEvent.ACTION_UP:
        {
            if(id == touch1.pointerID)
            {
                touch1.setCollideable(false);
                touch1.pointerID = -1;
                touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();
            }
            else if(id==touch2.pointerID)
            {
                touch2.setCollideable(false);
                touch2.pointerID = -1;
                touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();

            }
            break;
        }
        case MotionEvent.ACTION_POINTER_UP:
        {
            if(id == touch1.pointerID)
            {
                touch1.setCollideable(false);
                touch1.pointerID = -1;
                touch1.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch1.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();
            }
            else if(id==touch2.pointerID)
            {
                touch2.setCollideable(false);
                touch2.pointerID = -1;
                touch2.getPosition().x = e.getX(index)*GameWorldProperties.getWidthRatio();
                touch2.getPosition().y = e.getY(index)*GameWorldProperties.getHeightRatio();

            }
            break;
        }
        case MotionEvent.ACTION_MOVE:
        {
                Log.d("pointer",String.format("Move index=%s id=%s", index,id));

                for(int i=0;i<e.getPointerCount();i++)
                {
                    Log.d("pointer",String.format("i=%s id=%s",i,e.getPointerId(i)));

                    touch1.setCollideable(true);
                    touch1.getPosition().x = e.getX(i)*GameWorldProperties.getWidthRatio();
                    touch1.getPosition().y = e.getY(i)*GameWorldProperties.getHeightRatio();

                    touch2.setCollideable(true);
                    touch2.getPosition().x = e.getX(i)*GameWorldProperties.getWidthRatio();
                    touch2.getPosition().y = e.getY(i)*GameWorldProperties.getHeightRatio();
                }

            break;
        }
        default:
        {
            break;
        }
    }
Was it helpful?

Solution

Hope this helps. This is from Beginning Android Game Programming book.

public boolean onTouch(View v, MotionEvent event) {
    synchronized (this) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        int pointerId = event.getPointerId(pointerIndex);
        TouchEvent touchEvent;

        switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            touchEvent = touchEventPool.newObject();
            touchEvent.type = TouchEvent.TOUCH_DOWN;
            touchEvent.pointer = pointerId;
            touchEvent.x = touchX[pointerId] = (int) (event
                    .getX(pointerIndex) * scaleX);
            touchEvent.y = touchY[pointerId] = (int) (event
                    .getY(pointerIndex) * scaleY);
            isTouched[pointerId] = true;
            touchEventsBuffer.add(touchEvent);
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:
            touchEvent = touchEventPool.newObject();
            touchEvent.type = TouchEvent.TOUCH_UP;
            touchEvent.pointer = pointerId;
            touchEvent.x = touchX[pointerId] = (int) (event
                    .getX(pointerIndex) * scaleX);
            touchEvent.y = touchY[pointerId] = (int) (event
                    .getY(pointerIndex) * scaleY);
            isTouched[pointerId] = false;
            touchEventsBuffer.add(touchEvent);
            break;

        case MotionEvent.ACTION_MOVE:
            int pointerCount = event.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {
                pointerIndex = i;
                pointerId = event.getPointerId(pointerIndex);

                touchEvent = touchEventPool.newObject();
                touchEvent.type = TouchEvent.TOUCH_DRAGGED;
                touchEvent.pointer = pointerId;
                touchEvent.x = touchX[pointerId] = (int) (event
                        .getX(pointerIndex) * scaleX);
                touchEvent.y = touchY[pointerId] = (int) (event
                        .getY(pointerIndex) * scaleY);
                touchEventsBuffer.add(touchEvent);
            }
            break;
        }

        return true;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top