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;
}
}