Friday, June 28, 2013

II. The Gods Hand- Keyboard Input



To game is ought to update every input, input should be accepted at the Update Method of the class..
Here's what you need:

At the very top of your class, in the using statements. Make sure that
using Microsoft.Xna.Framework.Input; is used.
These allows us to manipulate input and enables us to use classes or constructors in input handling.

Next declare 2 keyboard states, at the top of your class:
KeyboardState currentKeyboardState, oldKeyboardState;
Here's what we want to do.
Every update process(60 runs/frame), at the very top of your update method. Get the keyboardstate, then at the very end of the class put the oldKeyboardState inheriting the value of the currentKeyboardState..

protected void Update(GameTime gameTime){
currentKeyboardState = Keyboard.GetState();

//DO LOGICAL GATES AND TEST HERE

 oldKeyboardState = currentKeyboardState;

}

so let's use the code above..

if (currentKeyboardState.IsKeyDown(Keys.Up) && oldKeyboardState.IsKeyDown (Keys.Up ))
            {
                texturePosition.Y-=10;
            }



explanation :
if currentkeyboardstate is pressing Key UPand oldkeyboardstate is pressing Key UP update Y position.
This allows us to update while it's pressed, it is use for commands that updates when a key is being pressed continually such as (movement)
There are to states we will used
    if (currentKeyboardState.IsKeyUp(Keys.O) && oldKeyboardState.IsKeyDown(Keys.O))
            {
                texturePosition = new Vector2(100, 100);
      }
explanation:
if currentkeyboardstate is has released Key Oand oldkeyboardstate is pressing Key O update Y position.
set the position to 100,100


now let's run the game and input some commands,
add this lines at your update method

   if (currentKeyboardState.IsKeyDown(Keys.Right) && oldKeyboardState.IsKeyDown(Keys.Right))
            {
                texturePosition.X += 10;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Left) && oldKeyboardState.IsKeyDown(Keys.Left))
            {
                texturePosition.X -= 10;
            }
    if (currentKeyboardState.IsKeyDown(Keys.Down) && oldKeyboardState.IsKeyDown(Keys.Down))
            {
                texturePosition.Y += 10;
            }
            if (currentKeyboardState.IsKeyUp(Keys.O) && oldKeyboardState.IsKeyDown(Keys.O))
            {
                texturePosition = new Vector2(100, 100);
            }


now we can move the texture in 4 directions depending on the input..




SOURCE CODE

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D texture;
        Vector2 texturePosition = Vector2.Zero;
        KeyboardState currentKeyboardState, oldKeyboardState;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {


            base.Initialize();
        }


        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);
            texture = Content.Load<Texture2D>("test");
            texturePosition = new Vector2(100, 100);
        }


        protected override void UnloadContent()
        {

        }
        protected override void Update(GameTime gameTime)
        {
            currentKeyboardState = Keyboard.GetState();

            if (currentKeyboardState.IsKeyDown(Keys.Up) && oldKeyboardState.IsKeyDown(Keys.Up))
            {
                texturePosition.Y -= 10;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Right) && oldKeyboardState.IsKeyDown(Keys.Right))
            {
                texturePosition.X += 10;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Left) && oldKeyboardState.IsKeyDown(Keys.Left))
            {
                texturePosition.X -= 10;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down) && oldKeyboardState.IsKeyDown(Keys.Down))
            {
                texturePosition.Y += 10;
            }
            if (currentKeyboardState.IsKeyUp(Keys.O) && oldKeyboardState.IsKeyDown(Keys.O))
            {
                texturePosition = new Vector2(100, 100);
            }
            oldKeyboardState = currentKeyboardState;
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            spriteBatch.Draw(texture, texturePosition, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }

    }