Introducing Airxbc 0.5

UPDATE December 30, 2012
This post is out of date. For information on the latest version of Airxbc, see this page.

Lately I've been working on a new native extension that makes it possible to control your Adobe AIR games using the Xbox 360 controller. I'm currently calling it Airxbc (Air Xbox controller - ha HA!) and this 0.5 release is pretty much just an alpha test to get something out there. It think its pretty cool and plan on putting more work into it in the relatively near future. If you're interested, check out the video and details below!

Airxbc is Windows only as it relies on the XInput library from Microsoft. This library ships with most recent versions of the Windows OS and is part of the DirectX SDK so there's a good chance you already have it.

How it works

To use Airxbc in your projects, you just need to download the ane file at the end of this article and add it to your AIR project. I developed the sample application using FlashDevelop and have included the project files below so you can download it and see how it all works. Note that projects using native extensions cannot be packaged as standard .air installers. Instead they must be compiled to a native executable (.exe on Windows).

Please see this page for more information about setting up native extensions in FlashDevelop.

The current release, 0.5, has support for all the buttons on the 360 controller except the big orb button in the middle. The extension's pollGamePad method returns all the data in a generic ActionScript object. This is something I intend to change in the near future, but for now, that's how it works. The object contains the following properties:

error - 0 if no errors are presentA - if not 0, the A button is being pressedB - if not 0, the B button is being pressedX - if not 0, the X button is being pressedY - if not 0, the Y button is being pressedleftShoulder - if not 0, the left shoulder button is being pressedrightShoulder - if not 0, the right shoulder button is being pressedback - if not 0, the back button is being pressedstart - if not 0, the start button is being presseddpadUp - if not 0, the dpad's up button is being presseddpadDown - if not 0, the dpad's down button is being presseddpadRight - if not 0, the dpad's right button is being presseddpadLeft - if not 0, the dpad's left button is being pressedleftTrigger - a value between 0 and 255 (255 = fully depressed, 0 = not pressed)rightTrigger - a value between 0 and 255 (255 = fully depressed, 0 = not pressed)leftStickX - a value between -32768 and 32768 (-32768 = all the way left)leftStickY - a value between -32768 and 32768 (-32768 = all the way down)rightStickX - a value between -32768 and 32768 (-32768 = all the way left)rightStickY - a value between -32768 and 32768 (-32768 = all the way down)

How to use

To use the extension simply create a new instance of it and call the methods as needed. The callable methods are pollGamePad, setVibration and dispose. Here's a quick example:

We covered the pollGamePad method earlier, but the setVibration and dispose methods are new. You call setVibration to utilize the 360 controller's rumble feature. The function takes in two arguments: the speed for the left motor and the speed for the right motor. These arguments should be a number between 0 and 65535 with 0 stopping the motors. The dispose method should be called when the extension is no longer needed.

Building an executable

As mentioned earlier, you can't build a normal .air file when using native extensions. If you want to build an exe from Flashdevelop you'll need to modify the Packager.bat file located in the bat directory. First, at the end of the set OUTPUT line, change .air to .exe. Then on the line where it says call adt, in between %SIGNING_OPTIONS% and %OUTPUT% add this: -target native. At the end of that same line add: -extdir extension/release/. It should look similar to this once complete: