Fun Infused Games  |   Smooth Operator RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    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


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