我的游戏中有一个奇怪的问题。我正在使用 2 个操纵杆,一个用于射击/瞄准,一个用于移动我的角色。出于某种原因,我的多点触控方法一次只记录一个动作。当我按下时,第二个指针被注册,但我ACTION_MOVE
只适用于第一个指针。这很奇怪,因为这意味着它确实需要多于一个指针,但它不能同时移动多于一个指针。我在 gamedev.stackexchange 上问过这个问题,它已经活跃了大约一周,得到了几个答案,但没有任何东西可以让它 100% 工作。我已经自己尝试了几个小时。
onTouch 方法的代码:
//global variables
private int movePointerId = -1;
private int shootingPointerId = -1;
public void update(MotionEvent event) {
if (event == null && lastEvent == null) {
return;
} else if (event == null && lastEvent != null) {
event = lastEvent;
} else {
lastEvent = event;
}
// grab the pointer id
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
int actionIndex = event.getActionIndex();
int pid = action >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int x = (int) event.getX(pid);
int y = (int) event.getY(pid);
String actionString = null;
switch (actionCode)
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
actionString = "DOWN";
try{
if(x > 0 && x < steeringxMesh + (joystick.get_joystickBg().getWidth() * 2)
&& y > yMesh - (joystick.get_joystickBg().getHeight()) && y < panel.getHeight()){
movingPoint.x = x;
movingPoint.y = y;
movePointerId = pid;
dragging = true;
//checks if Im pressing the joystick used for moving
}
else if(x > shootingxMesh - (joystick.get_joystickBg().getWidth()) && x < panel.getWidth()
&& y > yMesh - (joystick.get_joystickBg().getHeight()) && y < panel.getHeight()){
shootingPoint.x = x;
shootingPoint.y = y;
shootingPointerId = pid;
shooting=true;
//checks if Im pressing the joystick used for shooting
}
}catch(Exception e){
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
if( pid == movePointerId ){
movePointerId = -1;
dragging = false;
}
else if( pid == shootingPointerId ){
shootingPointerId = -1;
shooting=false;
}
actionString = "UP";
break;
case MotionEvent.ACTION_MOVE: // this is where my problem is
if( pid == movePointerId ) {
movingPoint.x = x;
movingPoint.y = y;
} else if( pid == shootingPointerId ) {
shootingPoint.x = x;
shootingPoint.y = y;
}
actionString = "MOVE";
break;
}
如果我打印actionString
并pid
显示移动时它只检查pid=0
,但是当我按下 ( ACTION_POINTER_DOWN
) 时,我可以看到它确实注册了另一个pid
,这真的让我感到困惑。
为了更清楚起见,当我按下第二个指针时,例如我的射击杆,它会占据我按下的位置,即使我同时移动另一个操纵杆,但它会一直停留在那里直到我放开了另一个操纵杆。进一步证明它确实注册了超过 1 次触摸和超过 1 次pid
。
如果您需要任何进一步的解释,请告诉我。