问题描述:

I am having trouble implementing multi-touch i have searched for a while. the current implementation works but interrupts other input. this code is part of my input controller which handles/draws input areas. the Motion Event is passed via this function from OnTouchEvent(),

I have read that you must use pointer id to properly keep track, but everytime i try implement it the pointer index is out of range.

i could really use some help

 public void handleInput(MotionEvent motionEvent,LevelManager l, SoundManager sound, Viewport vp){

int pointerCount = motionEvent.getPointerCount();

for (int i = 0; i < pointerCount; i++) {

int x = (int) motionEvent.getX(i);

int y = (int) motionEvent.getY(i);

if(l.isPlaying()) {

switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:

if (right.contains(x, y)) {

l.player.setPressingRight(true);

l.player.setPressingLeft(false);

} else if (left.contains(x, y)) {

l.player.setPressingLeft(true);

l.player.setPressingRight(false);

} else if (jump.contains(x, y)) {

l.player.startJump(sound);

} else if (shoot.contains(x, y)) {

if(l.player.pullTrigger()){

sound.playSound("shoot");

}

} else if (pause.contains(x, y)) {

l.switchPlayingStatus();

}

break;

case MotionEvent.ACTION_UP:

if (right.contains(x, y)) {

l.player.setPressingRight(false);

} else if (left.contains(x, y)) {

l.player.setPressingLeft(false);

}

break;

case MotionEvent.ACTION_POINTER_DOWN:

if (right.contains(x, y)) {

l.player.setPressingRight(true);

l.player.setPressingLeft(false);

} else if (left.contains(x, y)) {

l.player.setPressingLeft(true);

l.player.setPressingRight(false);

} else if (jump.contains(x, y)) {

l.player.startJump(sound);

} else if (shoot.contains(x, y)) {

if(l.player.pullTrigger()){

sound.playSound("shoot");

}

} else if (pause.contains(x, y)) {

l.switchPlayingStatus();

}

break;

case MotionEvent.ACTION_POINTER_UP:

if (right.contains(x, y)) {

l.player.setPressingRight(false);

} else if (left.contains(x, y)) {

l.player.setPressingLeft(false);

} else if (shoot.contains(x, y)) {

//Handle shooting here

} else if (jump.contains(x, y)) {

//Handle more jumping stuff here later

}

break;

}

}else {// Not playing

//Move the viewport around to explore the map

switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:

if (right.contains(x, y)) {

vp.moveViewportRight(l.mapWidth);

} else if (left.contains(x, y)) {

vp.moveViewportLeft();

} else if (jump.contains(x, y)) {

vp.moveViewportUp();

} else if (shoot.contains(x, y)) {

vp.moveViewportDown(l.mapHeight);

} else if (pause.contains(x, y)) {

l.switchPlayingStatus();

}

break;

}

}

}

}

网友答案:

OK i found an answer, using MotionEventCompat i was able to achieve the desired behaviour by adding an index and passing in the event through the MotionEventCompat interface.

        int index = MotionEventCompat.getActionIndex(motionEvent);
        int x = (int) MotionEventCompat.getX(motionEvent, index);
        int y = (int) MotionEventCompat.getY(motionEvent, index);

instead of int x = (int) motionEvent.getX(i); int y = (int) motionEvent.getY(i); right after

        for (int i = 0; i < pointerCount; i++) {

this seems like such a mistery to me to be honestly it feels really hacky, i'm not even sure if iterating through both pointers is necessary now. i am going to try and comment back

//edit the for loop is for sure redundant, i just had to make sure before i embarrassed myself further

相关阅读:
Top