Game Development Reference
In-Depth Information
public int
getTouchY(
int
pointer) {
synchronized
(
this
) {
return
touchY;
}
}
The methods
isTouchDown()
,
getTouchX()
, and
getTouchY()
allow us to poll the state of the
touchscreen based on the members that we set in the
onTouch()
method. The only noticeable
thing about them is that they only return useful data for a pointer ID with a value of zero, since
this class only supports single-touch screens.
public
List<TouchEvent>getTouchEvents() {
synchronized
(
this
) {
int
len=touchEvents.size();
for
(
int
i=0; i<len; i++ )
touchEventPool.free(touchEvents.get(i));
touchEvents.clear();
touchEvents.addAll(touchEventsBuffer);
touchEventsBuffer.clear();
return
touchEvents;
}
}
}
The final method,
SingleTouchHandler.getTouchEvents()
, should be familiar to you, and is
similar to the
KeyboardHandler.getKeyEvents()
methods. Remember that we call this method
frequently so that the
touchEvents
list doesn't fill up.
The MultiTouchHandler
For multitouch handling, we use a class called
MultiTouchHandler
, as shown in Listing 5-11.
Listing 5-11. MultiTouchHandler.java (More of the Same)
package
com.badlogic.androidgames.framework.impl;
import
java.util.ArrayList;
import
java.util.List;
import
android.view.MotionEvent;
import
android.view.View;
import
com.badlogic.androidgames.framework.Input.TouchEvent;
import
com.badlogic.androidgames.framework.Pool;
import
com.badlogic.androidgames.framework.Pool.PoolObjectFactory;
@TargetApi(5)
public class
MultiTouchHandler
implements
TouchHandler {
private static final int
MAX_TOUCHPOINTS
=10;