Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Clean Up Your Player Movement Code
Date 1/15/2009    Tags XNA    (0)

How many times have you had to enter a line in your game to handle player movement that looks something like this:

if ((keyboardState.IsKeyDown(Keys.A)) || 
   (gamePadState.DPad.Left == ButtonState.Pressed) || 
   (gamePadState.ThumbSticks.Left.X < 0))
It's long and awkward to read and can be a pain to change. Wouldn't you rather just do this?

if (LeftPressed(currentKeyboardState, currentGamePadState))
I know I would! And now, thanks to the magic of a couple very simple functions that I will display below, now you can!

private bool LeftPressed(KeyboardState keyboardState, GamePadState gamePadState)
{
    if (keyboardState.IsKeyDown(Keys.A)) 
    {
        return true;
    }

    if (gamePadState.DPad.Left == ButtonState.Pressed)
    {
        return true;
    }

    if (gamePadState.ThumbSticks.Left.X < 0)
    {
        return true;
    }

    return false;
}

private bool RightPressed(KeyboardState keyboardState, GamePadState gamePadState)
{
    if (keyboardState.IsKeyDown(Keys.D))
    {
        return true;
    }

    if (gamePadState.DPad.Right == ButtonState.Pressed)
    {
        return true;
    }

    if (gamePadState.ThumbSticks.Left.X > 0)
    {
        return true;
    }

    return false;
}

private bool UpPressed(KeyboardState keyboardState, GamePadState gamePadState)
{
    if (keyboardState.IsKeyDown(Keys.W))
    {
        return true;
    }

    if (gamePadState.DPad.Up == ButtonState.Pressed)
    {
        return true;
    }

    if (gamePadState.ThumbSticks.Left.Y > 0)
    {
        return true;
    }

    return false;
}

private bool DownPressed(KeyboardState keyboardState, GamePadState gamePadState)
{
    if (keyboardState.IsKeyDown(Keys.S))
    {
        return true;
    }

    if (gamePadState.DPad.Down == ButtonState.Pressed)
    {
        return true;
    }

    if (gamePadState.ThumbSticks.Left.Y < 0)
    {
        return true;
    }

    return false;
}
These four functions should handle all your basic movement needs (though will require some modification if you plan to take advantage of the analog nature of the thumbsticks).



This article has been view 883 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)