We have discussed how to get user input for both the Windows and Xbox platform, but how are we actually carrying out actions afterwards?
The team on Jojamian uses a simple procedure to handle clicking with either the mouse or GamePad. You can implement this in your 2D game as well.
For every time Update(); is called in Game.cs, we will check for whether the user has clicked, and respond appropriately.
In Game.cs’ Update(); include the following code;
if (clickEnabled)
{if (Game.previousGamePadState.Buttons.A == ButtonState.Pressed &&
Game.gamePadState.Buttons.A == ButtonState.Released)
{
switch (gameState)
{
case (int)State.Menu:
menu.aButtClick(ref gameState);
break;
case (int)State.Instructions:
instructions.aButtClick(ref gameState);
break;
}
clickEnabled = false;
}
Lets break this code down a bit ->
“if (clickEnabled)”
You will need to declare the bool clickEnabled in the Game class declaration and initialize it as true.
“switch (gameState)”
The gameState is the current state of the game. In this example there is a Menu and an Instruction state.
“menu.aButtClick(ref gameState);”
AND
“instructions.aButtClick(ref gameState)"”
The aButtClick(); function is called from the appropriate class corresponding to the current state. The aButtClick(); function should be placed within the class of that particular state. The content of aButtClick(); checks the position of the cursor (that we created in an earlier post) and verifies if it is over top of any significant button or action area. For each action area defined, you write the code that should run.
Has your team implemented a different method of responding to user clicks? Post them below and others can try all of the possible solutions to see what works best for them.