In this tutorial, I want to talk about how you can handle keyboard key presses and mouse button clicks. You already know how to track if a keyboard key or mouse button is down, but a lot of input checks need to only occur if a key is presses or a button is clicked.
Handling Keyboard Key Presses
What we need to do is keep a KeyboardState variable, that actually contains the data for the last Update loop. That way, if we need to have a check for key being down, all we have to do is make sure the current keyboard state is not equal to the last keyboard state. Then, after each Update, we set the value of the old keyboard to the value of the new keyboard. You may see how this works once we get started.
Step 1: Create an Old Keyboard State Variable
First we need to create a variable that holds the old state of the keyboard. Create this variable in the fields are:
KeyboardState myOldKeyboard = Keyboard.GetState();
As you see here, we are creating a keyboard state variable and naming it myOldKeyboard. Also, we are intializing it right now. This is because we are not going to initialize it at the beginning of the first update loop, so we should initialize it now. This is only going to matter if a key happens to be pressed right when the project starts(which would have to be in like the first 1.6 milliseconds or so).
Step 2: Update the Old KeyboardState at the End Of The Update Method
Now we need to update oldKeyboard at the end of the Update method. This is because, if a key happens to be pressed, and we don't keep track of that in the old KeyboardState variable, then oldKeyboard will not be exactly correct.
To update the old keyboard to the old state of the real keyboard, put this line at the end of the Update method:
myOldKeyboard = myKeyboard;
All this does is set the value of the oldKeyboard, to the value of the current keyboard(myKeyboard), which is actually old because it is used at the end of the Update method.
Note: When I say, "the end of the Update method", I always mean at the bottom of the Update method, but above the base.Update line.
Step 3: Test Key Presses
Now we get to test if what we have done so far actually works. In order to track a keyboard key press, we will check if the space bar is pressed. If the space bar has been pressed, we will taint our first sprite with a color of blue. If the space bar is pressed again, we will take the blue off of the color.
Put this code underneath the other keyboard input code for our first sprite:
if(myKeyboard.IsKeyDown(Keys.Space) &&
myOldKeyboard.IsKeyUp(Keys.Space))
{
spriteColored = !spriteColored;
}
Figure 1-1 The Code to Track a Key Press
First this code checks if currently, the space bar is pressed. Since myKeyboard is always set to the current state of the keyboard at the beginning of the Update method, if the space bar is pressed, this will be true. Then we also check(using the and(&&) operator) if the old state of the keyboard, which was set to a value at the end of the last Update call, does not have the space bar pressed. If both of these if checks return true, then we set a variable called spriteColored to its opposite. Now we just need to create this variable in the fields area:
bool spriteColored = false;
This creates a bool(a class that can have a value of either true or false) variable and initializes it with a value of false so that the sprite will start out normal.
Now we just need to make the sprite appear blue when spriteColored equals true.
In our Draw method, locate the call that draws the first sprite. Delete that and replace it with this code:
if(spriteColored)
{
mySpriteBatch.Draw(myTexture, spritePosition, Color.Blue);
}
else
{
mySpriteBatch.Draw(myTexture, spritePosition, Color.White);
}
This draws the sprite with a blue color if spriteColored equals true, and white if not. We use the else statement to signal that if spriteColored equals false, then the code inside the else statement will be executed.
Figure 1-2 Drawing Our Sprite Blue If SpriteColored Equals True
Note: Instead of using if(spriteColored == true), I can just put, if(spriteColored). Also, if(spriteColored == false) is the same as if(!spriteColored).
Now run the project. You should be able to press the space bar and the sprite will turn blue. You can press it again and the sprite will turn back to white.
Figure 1-3 The Sprite When it is Blue
That is how you keep track of a key press. Now time to check for mouse clicks.
Handling Mouse Button Clicks
Handling mouse clicks goes pretty much the same as handling keyboard key presses. To show when a mouse button is clicked, we will flip the second sprite using its SpriteEffects parameter when the right mouse button is clicked.
First, just like the KeyboardState variable, create a MouseState variable and initialize it:
MouseState myOldMouse = Mouse.GetState();
Then, at the bottom of the Update method, set it so that it always has the current, old value of the keyboard. In other words, so that it always is one Update method behind.
myOldMouse = myMouse;
Now we can check if the right button is clicked. Put this code under the mouse input code for the second sprite:
if(myMouse.RightButton == ButtonState.Pressed &&
myOldMouse.RightButton == ButtonState.Released)
{
sprite2Flipped = !sprite2Flipped;
}
Create the sprite2Flipped bool in the fields area:
bool sprite2Flipped = false;
Finally, flip the sprite when sprite2Flipped equals true. Delete the code that draws the second sprite and replace it with this:
if(sprite2Flipped)
{
mySpriteBatch.Draw(myTexture2, spritePosition2, null,
Color.White, 0, new Vector2(myTexture2.Width / 2,
myTexture2.Height / 2), 1, SpriteEffects.FlipHorizontally,
0);
}
else
{
mySpriteBatch.Draw(myTexture2, spritePosition2, null,
Color.White, 0, new Vector2(myTexture2.Width / 2,
myTexture2.Height / 2), 1, SpriteEffects.None, 0);
}
This code flips the sprite horizontally when sprite2Flipped equals true, and doesn't when sprite2Flipped is false. Now you can run the project. You should be able to click the right mouse button and the sprite should flip horizontally.
Now you know how to track keyboard key presses and mouse button clicks.
Figure 1-4 The Sprite When it is Flipped
If you have any requests or questions, please post a comment and I will try to answer. Thanks for reading the tutorial.
Remember:
You don't get to Heaven by what you do, but by what you believe.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment