Scratch for Java

Actions()

Named actions instead of single keys: bind keys, mouse buttons and controller buttons or sticks to an action like "jump", then ask for the action. The game works the same with keyboard and controller, and players can choose their own keys and buttons in an options menu.

Actions.bind("jump", KeyCode.SPACE, KeyCode.W);
Actions.bind("jump", GamepadButton.A);
Actions.bind("left", KeyCode.LEFT, KeyCode.A);
Actions.bind("left", GamepadAxis.LEFT_STICK_LEFT);
Actions.bind("right", KeyCode.RIGHT, KeyCode.D);
Actions.bind("right", GamepadAxis.LEFT_STICK_RIGHT);
 
// in run(): a stick gives everything between -1 and 1, keys -1, 0 or 1
this.changeX(5 * Actions.getAxis("left", "right"));
if (Actions.isPressed("jump")) { ... }

Each bind adds to what the action is bound to. An action is pressed while any of its keys or buttons is down, or a stick or trigger bound to it is pushed more than half-way. To be told when an action starts or ends, implement ActionListener on the stage or a sprite.

Every game also has six menu actions, which menus use and players can rebind too: ui_up, ui_down, ui_left, ui_right (arrow keys, WASD, D-pad and left stick), ui_accept (Enter, Space, A) and ui_back (Escape, Backspace, B).

Examples

View on GitHub

Actions()

Create Shareable URL

Select Sections