Responding to Gestures

To do all this magic, you need to become very friendly with the Gesture class in the Android SDK. Before you work with it in your code, let’s play around with gestures in a sample program created by the Android development team. You can access this app within the simulator: it’s called GestureBuilder. Before you do that, though, you have to make some changes to the simulator:

  1. GestureBuilder writes files to a tablet’s SD card. If your simulator doesn’t have this, you can add it easily. Choose Window images Android SDK and AVD Manager.
  2. Click the tablet device, and click the Edit button at left on the screen. A dialog box pops up (as shown in Figure 3-4) in which you type an amount of memory for the device. I usually use 1,000 megabytes.
    images

    Figure 3-4. Setting up the emulator for the GestureBuilder app

  3. Click Edit AVD.
  4. You’re brought back to the Android SDK and AVD Manager. Click the Start button; or, if the Start button isn’t available, first select the emulator name, as shown in Figure 3-5. You’re initiating the emulator from here because you want to be able to select the app rather than have an app start by default.
    images

    Figure 3-5. Launching the emulator from the Android SDK and AVD Manager

  5. When the emulator is up and running, go to the icon labeled Apps. Then, click the GestureBuilder program.
  6. Play around with this app for a bit to see how it works. Make a new gesture, and give it a name. Do a series of swipes to create your gesture. This application gives you a feel for what a gesture looks like.

If you created a truly great gesture that you want to use in a game, you can get the gesture from the SD card and reference it in your game code. This is an advanced topic, and you just want to experience gestures for now; to do this process, following the instructions in the Android documentation: http://developer.android.com/resources/articles/gestures.html.

Figure 3-6 shows a star gesture that I created. Even though you can draw a star in many ways, gestures are specific in that the order of the strokes is critical. The tablet is looking for the correct sequence.

images

Figure 3-6. Making a unique star gesture

The Android development group has its own program called GestureDemo that lets you perform gestures for the app to recognize. This chapter doesn’t go over the ins and outs of how the app works because it has limited relevance to most games. However, it’s worth looking at briefly because you can see how gestures are recognized. By changing your way of performing a gesture, you find out how accurate the tablet is at recognizing it:

7. Download the project from http://code.google.com/p/apps-for-android/downloads/detail?name=GesturesDemos.zip&can=2&q=. Unzip the folder, and be careful to remember where everything was extracted.

8. Open Eclipse, and choose File images Import images General images Existing Projects into Workspace.

9. Locate the folder GestureDemo that you downloaded, and fill out the form. When you click Finish, you have a new project in your workspace. To learn more about this project, you can visit the web page http://developer.android.com/resources/articles/gestures.html.

10. Run the new project, and begin performing gestures. If you do a lightning-bolt-type gesture, it should display Thunder Spell at the bottom.

If you play around with the project long enough, you may notice that it isn’t very accurate at recognizing some gestures. This is to be expected and is one reason it’s unusual to create custom gestures. The notable gestures that people are already familiar with, such as pinch and drag, are easily computed by Android and result in much less confusion. Nonetheless, some games are very exciting when you can use your hands to perform actions just as you would in real life. You can look up the Gestures library in the Android documentation to understand gestures better because it is too complicated of a topic to completely cover in this chapter.

images Tip If you create your own gestures, make them simple and exaggerated. Also, limit your game to one or two new gestures that are very, very different from each other, to avoid mistakes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.55.75