Game Development Reference
In-Depth Information
Listing 5-10. SingleTouchHandler.java; Good with Single Touch, Not So Good with Multitouch
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.Pool;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Pool.PoolObjectFactory;
public class SingleTouchHandler implements TouchHandler {
boolean isTouched;
int touchX;
int touchY;
Pool<TouchEvent>touchEventPool;
List<TouchEvent>touchEvents= new ArrayList<TouchEvent>();
List<TouchEvent>touchEventsBuffer= new ArrayList<TouchEvent>();
float scaleX;
float scaleY;
We start by letting the class implement the TouchHandler interface, which also means that we
must implement the OnTouchListener interface. Next, we have three members that store the
current state of the touchscreen for one finger, followed by a Pool and two lists that hold the
TouchEvent is This is the same as in the KeyboardHandler . We also have two members, scaleX
and scaleY . We'll address these in the following sections and use them to cope with different
screen resolutions.
Note Of course, we could make this more elegant by deriving the KeyboardHandler and
SingleTouchHandler from a base class that handles all matters regarding pooling and
synchronization. However, it would have complicated the explanation even more, so instead, we'll
write a few more lines of code.
public SingleTouchHandler(View view, float scaleX, float scaleY) {
PoolObjectFactory<TouchEvent>factory= new PoolObjectFactory<TouchEvent>() {
@Override
public TouchEvent createObject() {
return new TouchEvent();
}
};
touchEventPool= new Pool<TouchEvent>(factory, 100);
view.setOnTouchListener( this );
this .scaleX=scaleX;
this .scaleY=scaleY;
}
 
Search WWH ::




Custom Search