Skip to content

Instantly share code, notes, and snippets.

@zeh
Last active December 21, 2015 00:38
Show Gist options
  • Save zeh/6221357 to your computer and use it in GitHub Desktop.
Save zeh/6221357 to your computer and use it in GitHub Desktop.
KeyActionBinder examples
// Creating a KeyActionBinder instance with some bindings
// Declarations ---------
// Action constants for easy use
private static const ACTION_LEFT:String = "left";
private static const ACTION_RIGHT:String = "right";
private static const ACTION_JUMP:String = "jump";
// In your game start ---------
// Creates the instance
binder = new KeyActionBinder(stage);
// Creates typical keyboard bindings
var isAndroid:Boolean = Capabilities.manufacturer == "Android Linux";
if (isAndroid) {
// Android and OUYA: use native controller (supports OUYA, PS3, and XBox 360 controllers)
binder.addGamepadActionBinding(ACTION_LEFT, GamepadControls.DPAD_LEFT);
binder.addGamepadActionBinding(ACTION_RIGHT, GamepadControls.DPAD_RIGHT);
binder.addGamepadActionBinding(ACTION_JUMP, GamepadControls.BUTTON_ACTION_DOWN);
} else {
// PC: use keyboard and XBox 360 controller
binder.addKeyboardActionBinding(ACTION_LEFT, Keyboard.LEFT);
binder.addKeyboardActionBinding(ACTION_LEFT, Keyboard.A);
binder.addKeyboardActionBinding(ACTION_RIGHT, Keyboard.RIGHT);
binder.addKeyboardActionBinding(ACTION_RIGHT, Keyboard.D);
binder.addKeyboardActionBinding(ACTION_JUMP, Keyboard.SPACE);
binder.addKeyboardActionBinding(ACTION_JUMP, Keyboard.W);
binder.addGamepadActionBinding(ACTION_LEFT, GamepadControls.XBOX_DPAD_LEFT);
binder.addGamepadActionBinding(ACTION_RIGHT, GamepadControls.XBOX_DPAD_RIGHT);
binder.addGamepadActionBinding(ACTION_JUMP, GamepadControls.XBOX_BUTTON_ACTION_A);
}
// In your game loop ---------
if (binder.isActionActivated(ACTION_LEFT)) {
// Move the player to the left...
player.speedX = -10;
} else if (binder.isActionActivated(ACTION_RIGHT)) {
// Move the player to the right...'
player.speedX = 10;
}
if (binder.isActionActivated(ACTION_JUMP) {
// Make player jump...
player.speedY = -10;
// "Consumes" the action so it won't be processed until the player presses it again
// This just deactivates currently activated actions
binder.consumeAction(ACTION_JUMP);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment