jetlag2d
    Preparing search index...

    Class GamepadDevice

    GamepadDevice represents a single controller slot (there are up to four, matching the browser's navigator.getGamepads() indices -- see Stage.gamepads). Unlike KeyboardDevice, there's no browser event for "a gamepad button was pressed" -- the API is poll-only, so poll() is called once per render tick (see Stage.pollGamepads), regardless of whether the world, HUD, or an overlay is what's currently active. That last part matters: it's what lets a pause overlay register its own gamepad handlers (e.g. A to resume) the same way it can register keyboard ones, instead of input going dead the moment a menu opens.

    Index
    connected: boolean = false

    Is a controller currently connected in this slot?

    index: number

    The slot this device represents (0-3), matching navigator.getGamepads()'s indices * *

    leftStick: { x: number; y: number } = ...

    The left analog stick, each axis in [-1, 1]

    Type Declaration

    • x: number

      Left (-1) to right (1)

    • y: number

      Up (-1) to down (1)

    leftTrigger: number = 0

    The left trigger's analog value, in [0, 1] (0 if the controller only reports it as a digital button)

    rightStick: { x: number; y: number } = ...

    The right analog stick, each axis in [-1, 1]

    Type Declaration

    • x: number

      Left (-1) to right (1)

    • y: number

      Up (-1) to down (1)

    rightTrigger: number = 0

    The right trigger's analog value, in [0, 1] (0 if the controller only reports it as a digital button)

    • Report whether button is currently held down, as of the last poll. setButtonPressHandler deliberately only fires once, on the down transition (see its docstring), so for a digital button that needs hold/repeat semantics -- the gamepad equivalent of holding an arrow key -- poll this every tick (e.g. from Scene.repeatEvents) instead, the same way leftStick/rightStick are already meant to be polled for analog hold-to-move input.

      Parameters

      Returns boolean

    • Set a handler to run when a button is pressed. Named "Press", not "Down", specifically because it's unlike KeyboardDevice.setKeyDownHandler: this does not re-fire while the button is held -- it fires once, on the down transition.

      Parameters

      • button: GamepadButton

        The button to listen for

      • handler: () => void

        The code to run

      Returns void