Touch Handlers

And now fragmentation hits us. In the last chapter we talked a little about the fact that multitouch is supported on Android versions greater than 1.6 only. All the nice constants we used in our multitouch code (e.g., MotionEvent.ACTION_POINTER_ID_MASK) are not available to us on Android 1.5 or 1.6. While we can use them in our code just fine if we set the build target of our project to an Android version that has this API, the application will crash on any device running Android 1.5 or 1.6. We want our games to run on all currently available Android versions, so how do we solve this problem?

We employ a simple trick. We write two handlers, one using the single-touch API of Android 1.5 and another using the multitouch API of Android 2.0 and above. As long as we don't execute the code of the multitouch handler on a device with a lower Android version than 2.0, we are safe. The code won't get loaded by the VM, and it won't throw exceptions like crazy. All we need to do is to find out which Android version the device is running and instantiate the proper handler. You'll see how that works when we discuss the AndroidInput class. For now let's concentrate on the two handlers.

Was this article helpful?

0 0

Post a comment