Controls System

A built-in system for linking specific inputs to general types of input events.

Note: The methods provided by this system are likely to change in future verisons of Crafty, as more input types are supported.

Events

TriggerInputDown [Data = {name}]
When a trigger group is activated
TriggerInputUp [Data = {name, downFor}]
When a trigger group is released
DirectionalInput [Data = {name, x, y}]
When a directional input changes

Methods

Back to top

.defineDpad()

defineDpad(string name, obj definition[, obj options])
name

a name for the dpad input

definition

an object which defines the inputs and directions for the dpad

options

a set of options for the dpad

A dpad is a type of directional control which maps a set of triggers to a set of directions.

The options object has two properties:

  • normalize (bool): If true, the directional input will be normalized to a unit vector. Defaults to false.
  • multipleDirectionBehavior (string): How to behave when multiple directions are active at the same time. Values are "first", "last", and "all". Defaults to "all".

Example

// Define a two-direction dpad, with two keys each bound to the right and left directions
Crafty.s("Controls").defineDpad("MyDpad", {
  {RIGHT_ARROW: 0, LEFT_ARROW: 180, D: 0, A: 180}
});
Back to top

.defineTriggerGroup()

defineTriggerGroup(string name, obj definition)
name

a name for the trigger group

definition

an object which defines the inputs for the trigger

A trigger group is a set of togglable inputs mapped to the same event. If any of the inputs are down, the trigger is considered down. If all are up, it is considered up. When the trigger state changes, a TriggerInputUp or TriggerInputDown event is fired.

The definition object lists the inputs that are mapped to the trigger:

  • keys: An array of Crafty keycodes
  • mouseButtons: An array of Crafty mouse button codes

Example

// Define a trigger group mapped to the left mouse button and the A and B keys.
Crafty.s("Controls").defineTriggerGroup("MyTrigger", {
  mouseButtons: [Crafty.mouseButtons.LEFT],
  keys: [Crafty.keys.A, Crafty.keys.B]
});