This is something that isn't specifically game programming stuff, which is why I am asking here. Basically, I have this code:
for (int i = 0; i < event.getPointerCount(); i++) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
if(event.getPointerId(i) == 0)
{
Log.d("Game", "Primary Down");
}
else if(event.getPointerId(i) == 1)
{
Log.d("Game", "Secondary Down");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
if(event.getPointerId(i) == 0)
{
Log.d("Game", "Primary Up");
}
开发者_如何转开发 else if(event.getPointerId(i) == 1)
{
Log.d("Game", "Secondary Up");
}
break;
}
}
Which someone else gave me saying that it would fix my original issue which was that if I put down the primary pointer it would be fine, and same for secondary pointer, but whatever the first finger that is released is considered the secondary pointer, but it brings up another issue, which I can't seem to figure out for some reason. Its kind of early here, so thats contributing, but still.
The issue I ran into with this code was that if the primary and secondary pointers are both down, and either of them are released, it activates both cases, so both "Primary Up" and "Secondary Up" are output, the second one released will recognize which one it is, if both cases go off before I get there, then it wouldn't matter at that point. Any idea how to fix something like this? Or another way to do the multitouch for a game, or anything for that matter. Thanks!
WWaldo
As far as I know, ACTION_UP and ACTION_POINTER_UP (and the same for XX_DOWN...) only get called once, for each pointer. So you don't need to do that special treatment, just use event.getX() and event.getY(). To get the corresponding pointerId, you should use
int index = event.getActionIndex();
int pointerId = event.getPointerId(index);
The only time I use the for loop is for the ACTION_MOVE event, which is called independently of which pointer moved (and in that case you indeed need to use the pointerId, getX(i) and getY(i) )
edit : getActionIndex was only added in API 8, so you might want to use
event.getAction() >> MotionEvent.ACTION_POINTER_INDEX_SHIFT
to get the action index.
精彩评论