Game Development Reference
In-Depth Information
// init multi touch screen
ArrayList<MultiTouchGesture> gestures = new ArrayList<MultiTouchGesture>();
int w = getWindow().getWindowManager().getDefaultDisplay().getWidth();
int h = getWindow().getWindowManager().getDefaultDisplay().getHeight();
// move: left half of the screen
gestures.add(
new MultiTouchGesture(eGestureType.MOVE, new Rect(0, 0, w / 2, h)));
// look right half
gestures.add(
new MultiTouchGesture(eGestureType.LOOK, new Rect(w / 2, 0, w, h)));
mtScreen = new MultiTouchScreen(gestures);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mtScreen.onTouchEvent(event);
return true;
}
}
TestActivity
initializes the gestures coordinates by obtaining the display width and height
(using
getWindow() getWindowManager() getDefaultDisplay()
). It then initializes two types
of gestures:
MOVE
with bounds on the left half of the screen, and
LOOK
with bounds on the
right half. The gestures are passed to the
MultiTouchScreen
constructor. Finally, when a
single or multi-touch event fires,
onTouchEvent
in the main activity will be called and the
event relayed to
MultiTouchScreen
for consumption. This will allow your game character
to move and look simultaneously in 3D space. This technique will be put to the test in the
Quake I and II chapters of this topic.
FINAL THOUGHTS ABOUT MULTI-TOUCH
Before you think about implementing complex multi-touch schemes on your game, you should be aware that
the Android Multitouch API can be buggy (full of bogus pointers and false coordinates) in old devices such as
the Motorola Droid 1 and first-generation phones. For example, when you touch and slide two fingers across
the screen and simply dump the
MotionEvent
coordinates on screen, you will get a ton of false coordinates
and bogus pointers, which can be very frustrating, especially for 3D shooters such as Quake. This is probably
due to cheap hardware or buggy kernel drivers. I am happy to report that in second- and third-generation
devices, such as the Droid 3, things have improved dramatically. In the latest Android SDK, multi-touch drivers
have improved even further to the point that if you have a device with decent hardware, you will probably not
encounter this type of multi-touch issue. More info about multi-touch can be found at
You have seen some neat tricks for handling audio, video, and input using keyboard, single, and multi-touch.
Let's wrap things up and see what is coming up in the next chapter.