Class Gamepad
java.lang.Object
org.openpatch.scratch.gamepad.Gamepad
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 Summary
Modifier and TypeMethodDescriptionstatic Vector2getStick(int player, GamepadStick stick) Where the player's controller's stick points, (0, 0) if it is not connected.static Vector2getStick(GamepadStick stick) Where the first connected controller's stick points, (0, 0) if there is none.static doublegetTrigger(int player, GamepadTrigger trigger) How far down the player's controller's trigger is, 0 if it is not connected.static doublegetTrigger(GamepadTrigger trigger) How far down the first connected controller's trigger is, 0 if there is none.static booleanisButtonPressed(int player, GamepadButton button) Whether the player's controller has the button down.static booleanisButtonPressed(GamepadButton button) Whether any controller has the button down.static booleanWhether any controller is connected.static booleanisConnected(int player) Whether a controller sits in the player's slot.
-
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
Whether any controller has the button down.- Parameters:
button- the button to ask about- Returns:
- whether it is down on any connected controller
-
isButtonPressed
Whether the player's controller has the button down.- Parameters:
player- the player, 1 to 4button- the button to ask about- Returns:
- whether it is down on that player's controller
-
getStick
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
Where the player's controller's stick points, (0, 0) if it is not connected.- Parameters:
player- the player, 1 to 4stick- the stick to ask about- Returns:
- its position, x −1 to 1 and y −1 to 1 (up is positive)
-
getTrigger
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
How far down the player's controller's trigger is, 0 if it is not connected.- Parameters:
player- the player, 1 to 4trigger- the trigger to ask about- Returns:
- how far it is pressed, 0 (not at all) to 1 (fully)
-