CHAPTER 3 ■ WORKING WITH 2D IMAGES/TEXTURES IN XNA 3.0
180
You see that we immediately set a default position for our image. Let’s use this position in
our Draw method:
spriteBatch.Draw(myTexture, position, Color.White);
This code should run without any problem, displaying the image at a fixed position. Next,
let’s read from our keyboard. Since this should be done frequently, it should be done in the
Update method. Place the following line in the Update method to read the current state of all
keys on the keyboard:
KeyboardState keyState = Keyboard.GetState();
Finally, we can use this keyState variable to check which keys are pressed and change our
image position accordingly:
if (keyState.IsKeyDown(Keys.Right))
position.X++;
if (keyState.IsKeyDown(Keys.Left)) ...