Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/ key-action-binder-as3 Public archive

A keyboard/gamepad binder for easier game input in ActionScript

License

Notifications You must be signed in to change notification settings

zeh/key-action-binder-as3

Repository files navigation

KeyActionBinder

KeyActionBinder tries to provide universal game input control for both keyboard and game controllers in Adobe AIR, independent of the game engine used or the hardware platform it is running in.

While Adobe Flash already provides all the means for using keyboard and game input (via KeyboardEvent and GameInput), KeyActionBinder tries to abstract those classes behind a straightforward, higher-level interface. It is meant to be simple but powerful, while solving some of the most common pitfalls involved with player input in AS3 games.

Using KeyActionBinderReferenceVersion ChangelogSupported devicesContribute

Goals

  • Unified interface for keyboard and game controller input
  • Made to be fast: memory allocation is kept to a minimum, and there are no device references or instances to maintain
  • Abstract actual controls in favor of action ids: easier to configure key bindings through variables, with redundant input types (keyboard and gamepad)
  • Automatic bindings on any platform by hiding away platform-specific controls over unified ids
  • Self-containment and independence from any other system or framework

Usage

private var binder:KeyActionBinder;

// Setup (first frame/root of SWF)
KeyActionBinder.init(stage);

public function setup():void {
	// Create instance
	binder = new KeyActionBinder();
	
	// Setup as many action bindings as you want
	binder.addKeyboardActionBinding("move-left", Keyboard.LEFT);
	binder.addKeyboardActionBinding("move-right", Keyboard.RIGHT);
	binder.addGamepadActionBinding("move-left", GamepadControls.DPAD_LEFT);
	binder.addGamepadActionBinding("move-right", GamepadControls.DPAD_RIGHT);
}

function function gameLoop():void {
	// Evaluate actions
	if (binder.isActionActivated("move-left")) {
		// ...
	} else if (binder.isActionActivated("move-right")) {
		// ...
	}
}

Read more in the guide.

Tests/demos

KeyActionBinderTester
KeyActionBinderTester
Web-based version, Android/OUYA APK
Source code

Read more

GameInput problems

In case of problems with KeyActionBinder... know that Flash's GameInput API is still severely ridden with bugs. You may run into some of them. Here's some more information.

I'll remove items from the list when they're fixed.

Credits and thanks

  • James Dean Palmer for the original idea about auto-mapping controls and many bindings
  • Patrick Bastiani for the NeoFlex controller mapping
  • Rusty Moyher for the Buffalo SNES mapping, and several other mappings for Windows and OSX
  • Eric Socolofsky for testing, bug fixes, and OSX mappings for the XBox 360 controller
  • mwgray for important bug fixes and features

License

KeyActionBinder uses the MIT License. You can use this code in any project, whether of commercial nature or not. If you redistribute the code, the license (LICENSE.txt) must be present with it.

To-do

  • Use caching samples? Change sampling rate?
  • Properly detect buttons that immediately send down+up events that cannot be detected by normal frames (e.g. HOME on OUYA)
  • Allow detecting "any" gamepad key (for "press any key")
  • More automatic gamepad mappings
  • Still allow platform-specific control ids?
  • Profile and test performance/bottlenecks/memory allocations
  • A better looking KeyActionBinderTester demo
  • Compile binary/stable SWC
  • More bulletproof support for 2+ controllers