0
我正在編寫一個應用程序,該應用程序在屏幕上有兩個軟性d-pad。我極其困難地試圖捕捉和理解兩個同時觸摸的所有觸摸事件,因爲事實是每個ACTION_MOVE事件都說ID等於0.與遊戲控制器有關的Android多點觸控問題
我所擁有的是兩個'touch'對象,當碰觸時碰撞到碰撞檢測系統。記住我有兩個d-pad,兩者都需要有能力同時控制。
有沒有人有任何建議。這是我的onTouchEvent方法。請記住,可能難以理解上下文,因爲我使用cusomt'touch'gameobject與d-pad進行碰撞檢測。
@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