Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The ButtonMap on the Xbox360GamePad allows for the Keyboard to simulate input for an Xbox360GamePad. By default this value is null, therefore the Keyboard will not simulate any input. Setting this value to a non-null value will enable keyboard simulation.
The following example sets up a button map for the keyboard:
The ButtonDown method returns whether a button is currently being held down. This will be false if the player is not pushing the button, and it will return true every frame that the button is pressed.
The following code shows how to perform logic if the player is holding the A or B buttons on the Xbox360GamePad:
Add the following using statements:
Later, perform the checks:
The DPad on the Xbox360GamePad acts as four separate buttons. Therefore, each direction can be independently checked just like buttons. For example, the following code moves a character according to the left and right DPad.
Buttons are represented by enumerations which can be looped through to check. The first enumeration value is 0, and the last value in the enumeration is Button.LeftStickAsDPadRight. Therefore, the following loop will check all buttons:
ButtonPosition can be used to reference a button based in its physical position rather than by letter. Referencing a button by position can be useful if players are playing your game with gamepads which do not necessarily position their buttons identically.
For example, consider the A, B, X, and Y button positions on the Xbox and Switch controllers.
You may want your game to use the button positions rather than specific button letters when assigning input to your player.
Entities typically use IPressableInput instances to control actions like jumping. Rather than inspecting the gamepad type, games cna use the ButtonPosition to assign input. For example, the following code might be used to assign the JumpInput:
The ButtonPushed method returns whether a particular button was pushed. A "push" is defined as occurring when a button is not down the previous frame, but is down the current frame.
The following code might be used to cause an Entity to jump when the A button is pushed:
Alternatively check the ButtonPosition.
The Deadzone property can be used to address drift which can occur due to analog stick hardware reporting near-zero values, or due to the user unintentionally pushing the analog slightly in an undesired direction. By default, Xbox360GamePads use a 0.1f Deadzone value. The details of how the Deadzone property is applied can be found in the DeadzoneType page.
The ControlPositionedObject method is a shortcut method which can be used to quickly add control logic for PositionedObjects using a Xbox360GamePad. The ControlPositionedObject method modifies the Velocity values of a PositionedObject. Therefore, consider the following:
Acceleration will not have any impact on a PositionedObject that is being controlled by ControlPositionedObject
ControlPositionedObject sets Velocity even if the left analog stick is not being touched (it will set it to 0).
ControlPositionedObject on Entities which contain shapes will break CollideAgainstBounce calls since CollideAgainstBounce modifies velocity, but the modified velocity will be overridden by ControlPositionedObject. If using CollideAgainstBounce, consider using ControlPositionedObjectAcceleration.
ControlPositionedObject provides control for X, Y, and Z (using the shoulder buttons).
Note: ControlPositionedObject is a method which can be very useful for new programmers, for prototypes, and for simple games. However, most games require more custom controls over their objects. Therefore, you may find that you will need to replace ControlPositionedObject with custom movement code as your project expands. If so, don't worry - you're not doing anything wrong.
Assuming MyEntity is a valid PositionedObject instance:
The following code is the code used in the engine to implement ControlPositionedObject. You can use and modify this code if you'd like to customize how the logic is applied:
The CreateDefaultButtonMap method is a method which creates a button map for the calling Xbox360GamePad. This is a quick way to bind Keyboard keys to the Xbox360Controller - especially as a fallback during development if an Xbox360 game pad is not connected. For more information on creating custom button maps, see the Xbox360GamePad ButtonMap page.
The CreateDefaultButtonMap method performs the following logic internally:
You can modify the ButtonMap after it's been created:
For more information, see the ButtonMap page.
The name Xbox360GamePad exists for historical reasons - XNA originally only exposed support for Xbox36 controllers. Modern versions of FlatRedBall provide support for a variety of hardware including:
Xbox360 and Xbox One controllers
Switch Pro controllers
USB Gamecube controllers
Playstation DualShock
General PC controllers
The following code reports the number of game pads connected at a given time:
The Xbox360GamePad provides two analog sticks:
LeftStick
RightStick
The following code sets the vibration on the game pad:
The Xbox360GamePad name orignally comes from the fact that XNA supported Xbox 360 controllers. Despite its name, the Xbo360GamePad class can be used on most FlatRedBall platforms. As of the time of this writing, the following platforms and hardware are supported.
Desktop GL: Xbox 360 controllers, Xbox One controllers, Switch Pro controllers, USB Gamecube controllers, PlayStation DualShock, Generic PC controllers
Desktop XNA: Xbox 360 controllers, Xbox One controllers
Android: Bluetooth and wired controllers
iOS: Untested
The Xbox360GamePad class represents the current state of a physical game pad. The Xbox360GamePad can provide information about its buttons and DPad with ButtonPushed, ButtonDown, and ButtonReleased methods. You can also get information about the analog sticks using the references. Information about the trigger buttons can be accessed through the references. The exposes an array of Xbox360GamePads. There are always 4 elements in this array regardless of the number of game pads connected.
The Xbox360GamePad class reports whether a button was pushed the last frame, is currently down, or was released. The following code moves a to the right one unit when the A button is pressed and moves a to the left when the B button is released:
Each property provides information about the matching hardware analog stick. For more information on using the analog stick property, see the page.
See the .
The Xbox360GamePads which are part of the are automatically updated every frame by the engine. This information is not buffered, therefore very rapid button presses may not be ready by the controller.
The FakeIsConnected property can force a Xbox360GamePad's IsConnected property to true whether a physical controller is actually connected or not.
Some game logic may depend on the number of connected Xbox360GamePads. For example, a game which supports a local versus mode may enable one character-selection cursor for each connected Xbox360GamePad. You may not have access to four Xbox controllers, but you may want to still test this functionality. The FakeIsConnected enables this.
The DeadzoneType property controls how the Deadzone value is applied on an Xbox360GamePad. By default Radial DeadzoneType is used.
The following code sets the first Xbox360GamePad to use a Cross deadzone with a 0.15 value:
The Radial deadzone type treats the position of the analog stick as (0,0) if the position reported by the hardware is within a circle centered at (0,0) with the Deadzone value used as the radius. Visually, this means that any value within the red area would be reduced to (0,0).
Radial deadzones are recommended for top-down (four-way directional) movement games.
The Cross deadzone type treats each axis independently regardless of the value of the other axis. In other words, if the absolute value of X as reported by the hardware is smaller than the Deadzone value, then X will be reported as 0. Visually, this creates a cross deadzone area as shown in the following diagram:
Cross deadzones are recommended for platformer games and games where players may want to move perfectly along a particular axis.
The GamepadLayout property returns the type of layout that the Xbox360GamePad returns. This property is set according to the ID and Name reported by the gamepad. Note that this is not always accurate as some manufacturers reuse IDs and Names for different gamepads. If you have a gamepad which is incorrectly reported, please report this in the FlatRedBall chat or create a pull request in the Xbox360GamePad.cs file. Note that the InputManager includes an array of Xbox360GamePad objects for historical and convenience reasons - even controllers which are not Xbox360 (or Xbox One) controllers will appear in this list, such as PlayStation Dual Shock controllers.
The following code could be used to select an icon for a gamepad based on the gamepad layout.
The IgnoreButtonForOneFrame method can be used if multiple objects are watching for a particular button to be pushed, but the first one that reacts to it should "consume" it so that no other objects get the button push. This method is very convenient because it does not require any additional logic aside from simply calling this method when consuming the button event.
In this example we have some if statements which increment a number according to button pushes. The first check for the A button will call IgnoreButtonForOneFrame on the A button. The first check for the B button will not. The result is that A will increment the call count by 1, while B will increment the call by 2.
Add the following using statement:
Add the following at class scope:
Add the following in Update: