Class Gamepad

java.lang.Object
org.openpatch.scratch.gamepad.Gamepad

public final class Gamepad extends Object
Asks the connected controllers, like a gamepad's blocks in other Scratch dialects. Players are numbered 1 to 4 in the order the controllers sit in the system's joystick slots; a controller without a known layout (an unmapped pad) is skipped. Without a player number, every method looks at all controllers: isButtonPressed(GamepadButton) is true if any controller has the button down, and getStick(GamepadStick) and getTrigger(GamepadTrigger) read the first connected controller.

Sticks are a Vector2 with x from −1 (left) to 1 (right) and y from −1 (down) to 1 (up), as on the stage; near the middle they report (0, 0). Triggers go from 0 (not pressed) to 1 (fully pressed). Buttons are in Xbox positions; see GamepadButton for the PlayStation equivalents. Controllers connect and disconnect while the game runs; to react to that and to button presses, implement GamepadListener on the stage or a sprite.

Before a window exists or while no controller is connected, queries return false, zero or (0, 0), so a game can ask without checking first.

  • Method Details

    • isConnected

      public static boolean isConnected()
      Whether any controller is connected.
    • isConnected

      public static boolean isConnected(int player)
      Whether a controller sits in the player's slot.
      Parameters:
      player - the player, 1 to 4
      Returns:
      whether that slot has a controller
    • isButtonPressed

      public static boolean isButtonPressed(GamepadButton button)
      Whether any controller has the button down.
      Parameters:
      button - the button to ask about
      Returns:
      whether it is down on any connected controller
    • isButtonPressed

      public static boolean isButtonPressed(int player, GamepadButton button)
      Whether the player's controller has the button down.
      Parameters:
      player - the player, 1 to 4
      button - the button to ask about
      Returns:
      whether it is down on that player's controller
    • getStick

      public static Vector2 getStick(GamepadStick stick)
      Where the first connected controller's stick points, (0, 0) if there is none.
      Parameters:
      stick - the stick to ask about
      Returns:
      its position, x −1 to 1 and y −1 to 1 (up is positive)
    • getStick

      public static Vector2 getStick(int player, GamepadStick stick)
      Where the player's controller's stick points, (0, 0) if it is not connected.
      Parameters:
      player - the player, 1 to 4
      stick - the stick to ask about
      Returns:
      its position, x −1 to 1 and y −1 to 1 (up is positive)
    • getTrigger

      public static double getTrigger(GamepadTrigger trigger)
      How far down the first connected controller's trigger is, 0 if there is none.
      Parameters:
      trigger - the trigger to ask about
      Returns:
      how far it is pressed, 0 (not at all) to 1 (fully)
    • getTrigger

      public static double getTrigger(int player, GamepadTrigger trigger)
      How far down the player's controller's trigger is, 0 if it is not connected.
      Parameters:
      player - the player, 1 to 4
      trigger - the trigger to ask about
      Returns:
      how far it is pressed, 0 (not at all) to 1 (fully)