java - Android MultiTouch does not work -
@override public boolean ontouchevent(motionevent event) { synchronized (getholder()) { int aktion = event.getaction(); if (aktion == motionevent.action_down) { touched = true; bar.sharetouch(event.getx(), event.gety(), touched); } else if (aktion == motionevent.action_up) { touched = false; bar.sharetouch(event.getx(), event.gety(), touched); } if (aktion == motionevent.action_pointer_down) { touched2 = true; bar.sharetouch2(event.getx(), event.gety(), touched2); } else if (aktion == motionevent.action_pointer_up) { touched2 = false; bar.sharetouch2(event.getx(), event.gety(), touched2); } } return true; }
this code chech if first finger going onto screen or leaving it. same finger.
public void sharetouch2(float xtouch2, float ytouch2, boolean touched2) { if (xtouch2 <= gameviewwidth/2) { if (touched2 == true) { touchedleft2 = true; } else if(touched2 == false) { touchedleft2 = false; } } else if (xtouch2 > gameviewwidth/2) { if (touched2 == true) { touchedright2 = true; } else if(touched2 == false) { touchedright2 = false; } } }
public void sharetouch(float xtouch, float ytouch, boolean touched) { if (xtouch <= gameviewwidth/2) { if (touched == true) { touchedleft = true; } else if(touched == false) { touchedleft = false; } } else if (xtouch > gameviewwidth/2) { if (touched == true) { touchedright = true; } else if(touched == false) { touchedright = false; } } } private void moveright() { x += 3; } private void moveleft() { x -= 3; }
private void checktouch() { if ((touchedleft == true && touchedright2 == false) || (touchedleft2 == true && touchedright == false)) { moveleft(); } else if ((touchedleft == false && touchedright2 == true) || (touchedleft2 == false && touchedright == true)) { moveright(); } else if ((touchedleft == true && touchedright2 == true) || (touchedleft2 == true && touchedright == true)) { } }
the checktouch()
called in ondraw()
method. if place finger on right side of screen moves right. same left. if touch left , right without removing left finger object still moves left although should stop. when leave left finger still moves left although should move right.
i hope understand problem.
hope can help
you can't assume action_pointer_up
event sent same finger action_pointer_down
sent.
just action_down
event fired first finger touch down, action_up
event fired last finger lift (i.e. when there no more fingers on screen). others action_pointer_up
event, if it's finger started gesture.
however, android documentation specify
each pointer has unique id assigned when first goes down [and] remains valid until pointer goes up.
so in theory, first , second finger should still have same id, regardless whether event reports them.
the solution simple, then: use getpointercount() , getpointerid() keep track of fingers.
this may easier if refactor code account more 2 fingers replacing booleans touchedleft
, touchedleft2
single counter, e.g. fingersonleftside
- has neat side effect of needing 1 sharetouch()
method , otherwise reducing redundancy in code.
edit:
disclaimer: off top of head, untested , written without knowledge of of code other posted. not best way solve problem, shortest think of.
this event handler:
public boolean ontouchevent(motionevent event) { synchronized (getholder()) { int aktion = event.getaction(); if (aktion == motionevent.action_down || aktion == motionevent.action_pointer_down || aktion == motionevent.action_up || aktion == motionevent.action_pointer_up) { bar.cleartouches(); (int = 0; < event.getpointercount(); i++) { bar.sharetouch(event.getx(i)); // don't need y coordinate anyway } } } return true; }
the rewritten sharetouch()
go along it, cleartouches()
introduced make simpler:
private void sharetouch(float x) { if (x < gameviewwidth/2) { fingersleft++; } else { fingersright++; } } private void cleartouches() { fingersleft = 0; fingersright = 0; }
and finally, new checktouch()
:
public void checktouch() { if (fingersleft > fingersright) { moveleft(); } else if (fingersright > fingersleft) { moveright(); } }
Comments
Post a Comment