Skip to content

Buttons and Analog Sticks

Samir Zahran edited this page May 18, 2017 · 2 revisions

Buttons and Analog Sticks both have a set of events used to report different kinds of changes in those inputs' states.

Getting Input Information from an Event

Each input event will have the corresponding input object made available through the details attribute of the Event object.

// Button press event
window.addEventListener('gc.button.press', function(event) {
    var button = event.detail;
    console.log(button);
}, false);

>> Button {
>>     controllerIndex: 0,
>>     time: 4526.165,
>>     name: "DPAD_UP",
>>     pressed: true,
>>     value: 1
>> }

All Button Events
→ [[Details about Button objects|Button Events#button-objects]]

// Analog Stick start movement event
window.addEventListener('gc.analog.start', function(event) {
    var stick = event.detail;
    console.log(stick);
}

>> AnalogStick {
>>     controllerIndex: 0,
>>     time: 4526.165,
>>     name: "LEFT_ANALOG_STICK",
>>     position: { x: -0.3102, y: 0 },
>>     angle: { degrees: 180, radians: 3.142 }
>> }

All Analog Stick Events
→ [[Details about AnalogStick objects|Analog Stick Events#analogstick-objects]]

"Pausing" a Controller

In the case that you have multiple controllers connected, and for a period of time you want to ignore all events fired from one of them, you can use the .watch() and .unwatch() methods.

var controller = Controller.getController(0);

controller.unwatch();
// No button or analog stick events will fire for this controller

controller.watch();
// Events will resume firing for this controller

The Controller object also has .watchAll() and unwatchAll() methods for ignoring and resuming events from all controllers.

Clone this wiki locally