Game Development Reference
In-Depth Information
The TouchHandler Interface
In order to use our two handler classes interchangeably, we need to define a common interface.
Listing 5-9 presents the TouchHandler interface.
Listing 5-9. TouchHandler.java, to Be Implemented for Android 1.5 and 1.6
package com.badlogic.androidgames.framework.impl;
import java.util.List;
import android.view.View.OnTouchListener;
import com.badlogic.androidgames.framework.Input.TouchEvent;
public interface TouchHandler extends OnTouchListener {
public boolean isTouchDown(int pointer);
public int getTouchX( int pointer);
public int getTouchY( int pointer);
public List<TouchEvent>getTouchEvents();
}
All TouchHandler is must implement the OnTouchListener interface, which is used to register the
handler with a View . The methods of the interface correspond to the respective methods of the
Input interface defined in Chapter 3. The first three are for polling the state of a specific pointer
ID, and the last is for getting TouchEvent is with which to perform event-based input handling.
Note that the polling methods take pointer IDs that can be any number and are given by the
touch event.
The SingleTouchHandler Class
In the case of our single-touch handler, we ignore any IDs other than zero. To recap, we'll recall
the TouchEvent class defined in Chapter 3 as part of the Input interface.
public static class TouchEvent {
public static final int TOUCH_DOWN =0;
public static final int TOUCH_UP =1;
public static final int TOUCH_DRAGGED =2;
public int type;
public int x, y;
public int pointer;
}
Like the KeyEvent class, the TouchEvent class defines a couple of constants that echo the touch
event's type, along with the x and y coordinates in the coordinate system of the View and the
pointer ID. Listing 5-10 shows the implementation of the TouchHandler interface for Android 1.5
and 1.6, broken up by commentary.
 
Search WWH ::




Custom Search