by Nimisha

Nimisha

Adobe

 

Table of contents

Created

29 May 2014

Requirements
Prerequisite knowledge
ActionScript 3.0
Flash Player
AIR Gamepad API
Required products
Adobe AIR SDK
Flash Player
Sample files
HungryHero.fxp (6392 KB)
User Level
Intermediate

 
Introduction

Modern day technology presents several unique uses for today’s advanced smart phones. Adobe’s AIR 15.0 offers a restructured AIR Gamepad API that extends the use of Android devices as game controllers and second-screen interfaces. You can use the AIR Gamepad API to connect Flash-based browser applications to a paired Android device running the AIR runtime application.
 
With the AIR Gamepad API you can:
 
  • Connect to an AIR runtime application and load the second-screen content to the AIR runtime application’s Gamepad view.
  • Fetch accelerometer events from the mobile device.
  • Request the AIR runtime application to provide vibration feedback.
  • Apply a skin to the screen of the paired mobile device.
  • Fetch gestures (swipe, zoom, rotate, pan, and two-finger tap) from the Android device.
  • Fetch touch events (begin, move, and end) from the Android device.
  • Fetch magnetometer events from the mobile device. (Introduced in AIR 15.0 beta).
  • Fetch gyroscope events from the mobile device. (Introduced in 15.0 beta).
  • Provide multi player support for your games. This means that more than one device can be paired with the Flash-based browser game. (Introduced in 15.0 beta).
To download the SWC file, go to AIR 15.0 Beta and download the AIR SDK. The SWC file is located at AIR SDK\frameworks\libs\gamepad.swc.
 
See the API documentation for a detailed description of the API and its applications.
 

 
Working with the Gamepad API

To begin setting up a connection between the game application and the android device:
 
  1. Create an AIRGamepad object using the getGamepad(gamepadID:String) static method of the AIRGamepad class.
  2. Call the connect(stage:Stage, publisher:String,applicationURL:String) function of the AIRGamepad class.
  3. On calling the connect() function from the Flash Player application, the user is prompted for a PIN.
    The PIN is the alphanumeric string displayed on the AIR Gamepad screen on the Android device. The PIN is required to establish a connection with the Android device running the AIR runtime application. See Figure 1.
Figure 1: User prompt for PIN when the Flash Player game is launched in a browser and the connect() function is called.
Figure 1: User prompt for PIN when the Flash Player game is launched in a browser and the connect() function is called.
 
In order to complete the connection from the game application to the Android device the user needs to:
 
1. Install the Adobe AIR runtime application on the Android device from AIR SDK\runtimes\air\android\device\Runtime.apk, if not already installed.
 
2. Launch the Adobe AIR runtime application.
 
3. Shake the device. The AIR Gamepad screen is displayed in the AIR runtime application, with a PIN. See Figure 2.
 
Figure 2: The AIR Gamepad screen shown by AIR runtime App on Android device
Figure 2: The AIR Gamepad screen shown by AIR runtime App on Android device
 
4. Enter the PIN in browser pop-up and click Connect.
 
Note: Both the Android device and the desktop computer need to be on the same internet network to be able to connect through the AIR Gamepad APIs.
 
Call drawImage(encodedImage:ByteArray) of the AIRGamepad class as soon as the connection between the game application and the android device has been established. This will ensure that that the skin is displayed immediately on the Android device’s application screen and the user can begin to control the Flash-based browser game from the Android device. See Figure 3 for an example of the skin displayed for the Flash-based game, HungryHero.
 
To download the swc file go to AIR 15.0 Beta and download the AIR SDK. The SWC file is located at AIR SDK\frameworks\libs\gamepad.swc.
 
Figure 3: HungryHero skin applied to the AIR Gamepad on Android device
Figure 3: HungryHero skin applied to the AIR Gamepad on Android device
 
You can only use images in the JPEG format for the Gamepad skin. The DRAW_ERROR  error occurs when any format other than JPEG is used.
 
Note: Only the rotated-left landscape orientation is supported for Gamepad on Android devices, which is also applicable to the skin.
 
When a connection between the game application and the android device is successfully established and the skin is applied, the user can begin to use the Android device as a game controller.
 
HungryHero is an example of a Flash-based game that uses the Gamepad API. Accelerometer events are fetched from the Android device to decide the movements of HungryHero. The Android device vibrates when HungryHero collides with objects or eats food. The vibrate(duration:int) function of the AIRGamepad class is used to vibrate the device. You can also fetch gesture and touch events and take suitable action.
 

 
Where to go from here

You can try some of our Gamepad samples: HungryHero and ModelViewer. The source file for HungryHero is attached with this article.