Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't just assume the <code>ACTION_POINTER_UP</code> Event will only be sent for the same finger <code>ACTION_POINTER_DOWN</code> was sent.</p> <p>Just as the <code>ACTION_DOWN</code> event is always fired for the first finger to touch down, the <code>ACTION_UP</code> event is only fired for the <em>last</em> finger to lift (i.e. when there are no more fingers on the screen). All others will get an <code>ACTION_POINTER_UP</code> event, even if it's the finger that started the gesture.</p> <p>However, the Android documentation does <a href="http://developer.android.com/reference/android/view/MotionEvent.html" rel="nofollow">specify</a></p> <blockquote> <p>Each pointer has a unique id that is assigned when it first goes down [and] remains valid until the pointer eventually goes up.</p> </blockquote> <p>So in theory, your first and second finger should still have the same ID, regardless whether the Event that reports them.</p> <p>The solution is simple, then: Use <a href="http://developer.android.com/reference/android/view/MotionEvent.html#getPointerCount%28%29" rel="nofollow">getPointerCount()</a> and <a href="http://developer.android.com/reference/android/view/MotionEvent.html#getPointerId%28int%29" rel="nofollow">getPointerID()</a> to keep track of your fingers.</p> <p>This may be easier if you refactor your code to account for more than two fingers by replacing the Booleans <code>touchedLeft</code> and <code>touchedLeft2</code> with a single counter, e.g. <code>fingersOnLeftSide</code> - which has the neat side effect of only needing one <code>shareTouch()</code> method and otherwise reducing redundancy in your code.</p> <p><strong>EDIT:</strong></p> <p>Disclaimer: This is just off the top of my head, untested and written without knowledge of any of your code other than what you posted. Not necessarily the best way to solve your problem, but the shortest I could think of.</p> <p>This is the Event handler:</p> <pre><code>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(); for (int i = 0; i &lt; event.getPointerCount(); i++) { bar.shareTouch(event.getX(i)); // we don't need the y coordinate anyway } } } return true; } </code></pre> <p>The rewritten <code>shareTouch()</code> to go along with it, as well as <code>clearTouches()</code> I introduced to make this simpler:</p> <pre><code>private void shareTouch(float x) { if (x &lt; gameViewWidth/2) { fingersLeft++; } else { fingersRight++; } } private void clearTouches() { fingersLeft = 0; fingersRight = 0; } </code></pre> <p>And finally, the new <code>checkTouch()</code>:</p> <pre><code>public void checkTouch() { if (fingersLeft &gt; fingersRight) { moveLeft(); } else if (fingersRight &gt; fingersLeft) { moveRight(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload