Я пишу приложение с двумя мягкими d-пэдами на экране. Я испытываю крайние трудности, пытаясь захватить и понять все события касания для двух одновременных касаний, потому что тот факт, что каждое событие ACTION_MOVE говорит о том, что идентификатор равен 0.Проблема с Android Multitouch с игровыми контроллерами
У меня есть два «сенсорных» объекта, которые получают каналы в систему обнаружения столкновения, когда есть прикосновение. Помните, что у меня есть два d-пэда, и оба они должны иметь возможность контролировать одновременно.
У кого-нибудь есть предложения. Вот мой метод onTouchEvent. Имейте в виду, что может быть трудно понять контекст, потому что я использую игровой объект cusomt touch для обнаружения столкновений с d-пэдами.
@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;
}
}
Собственно, это действительно помогает! Это сказало мне, что вместо того, чтобы поддерживать ссылки на объекты касания, я должен просто создавать новую с каждым действием! Имеет смысл! Благодаря! – Matthew
Добро пожаловать. Я действительно рекомендую книгу. Я многому научился у него. – neo