Sequenced Input Sample
This sample takes a sequence of buffered input from a player's controller and compares the sequence to a list of possible moves. If the sequence matches a move, the move appears on the screen.
Sample Overview
This sample shows how to accept controller input only once per button press using buffered input, and then to use the input to perform a variety of special moves.
It is easy to detect when a single button is pressed, but some games want to detect sequences of presses that occur in a particular order. Holding down a button might perform a action different than quickly tapping it, or the sequence Up, Down, B, Up, Up entered in rapid succession might trigger a special move. To detect such things, this sample stores not only the current state of the input, but also a series of previous states. Whenever a new input is received, it is added to an input buffer. The input buffer then can be analyzed to detect interesting sequences of actions.
Sample Controls
This sample uses the following keyboard and gamepad controls.
| Action | Keyboard control | Gamepad control |
|---|---|---|
| Move directions | UP ARROW, DOWN ARROW, LEFT ARROW, RIGHT ARROW, UP ARROW + LEFT ARROW, UP ARROW + RIGHT ARROW, DOWN ARROW + LEFT ARROW, and DOWN ARROW + RIGHT ARROW | Left thumb stick and D-pad |
| Button presses | A, B, X, and Y | A, B, X, and Y |
| Exit | ESC or ALT+F4 | BACK |
How the Sample Works
The Sequenced Input Sample takes buffered input from up to four controllers, and stores each controller's input in its own array. During each update, the controller's array is compared to a list of button sequences representing special moves. If the controller's current sequences matches any of the moves in the list, the name of the move appears on screen next to the controller number. This move also represents the last move completed. It displays until you trigger a new move.
Game contains three primary members that create the bulk of the sample's functionality. InputManager is responsible for handling the buffering of input by checking the controller's current state with its state during the previous Update call. It also handles the merging of button presses. To complete some button combinations, several buttons must be pressed down at the same time. Because the input is buffered, you must be careful to press the buttons within a short time span so that they all register as one combined button press. Otherwise, it would be necessary for all buttons to be pressed at exactly the same time for Update to recognize that all presses occurred simultaneously. This would make simple button combinations difficult. Move stores a sequence of button presses, as well as a name. MoveList is composed of an array of Move objects, as well as the logic for detecting if a move has been performed.
Each time Update is called, the InputManagers check the controllers for new input. The current sequence of button presses is then checked against the list of possible moves. It starts with the longest move. If a move is completed, it is returned and drawn to this screen next to the controller number having completed it.
Extending the Sample
Many games, including fighting games, allow the player to perform different actions by pressing or holding a specified button. This can be trickier than one might think, but there some ways to implement it. One way would be to create a new class that stores a gamepad as well as variables to indicate whether a button has been held a specified length of time. This requires some changes to Update. Also, it requires that the variables used to keep track of the held buttons are updated and reset accurately in order to reflect the controller's current state.