Sign in to follow this  

Help with Walk/Sprint Code

This topic is 2227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Below is the code I use for my sprite when walking and sprinting. I was wondering if I could reduce the amount of code. Is there a more efficient way of coding walking and sprinting?

[code]
Vector2 motion = new Vector2();

if (InputHandler.KeyDown(Keys.LeftShift) & InputHandler.KeyDown(Keys.W))
{
sprite.CurrentAnimation = AnimationKey.Up;
motion.Y = (float)-1.4;
}
else if (InputHandler.KeyDown(Keys.W))
{
sprite.CurrentAnimation = AnimationKey.Up;
motion.Y = (float)-0.8;
}

if (InputHandler.KeyDown(Keys.LeftShift) & InputHandler.KeyDown(Keys.S))
{
sprite.CurrentAnimation = AnimationKey.Down;
motion.Y = (float)1.4;
}
else if (InputHandler.KeyDown(Keys.S))
{
sprite.CurrentAnimation = AnimationKey.Down;
motion.Y = (float)0.8;
}

if (InputHandler.KeyDown(Keys.LeftShift) & InputHandler.KeyDown(Keys.A))
{
sprite.CurrentAnimation = AnimationKey.Left;
motion.X = (float)-1.4;
}
else if (InputHandler.KeyDown(Keys.A))
{
sprite.CurrentAnimation = AnimationKey.Left;
motion.X = (float)-0.8;
}

if (InputHandler.KeyDown(Keys.LeftShift) & InputHandler.KeyDown(Keys.D))
{
sprite.CurrentAnimation = AnimationKey.Right;
motion.X = (float)1.4;
}
else if (InputHandler.KeyDown(Keys.D))
{
sprite.CurrentAnimation = AnimationKey.Right;
motion.X = (float)0.8;
}
[/code]

Share this post


Link to post
Share on other sites
[quote name='Key' timestamp='1323043443' post='4890537']
Below is the code I use for my sprite when walking and sprinting. I was wondering if I could reduce the amount of code. Is there a more efficient way of coding walking and sprinting?
[/quote]

You can remove all of those else statements by just keeping the value that is returned from InputHandler.KeyDown(Keys.LeftShift); in it's own variable isShiftDown (0 if it isn't and 1 if it is) and then having a speed modifier speedModifier = 0.8f; then add (speedModifier * isShiftDown) to your normal speed. It shift is down, the variable will be 1 and will add the speed modifier to your speed otherwise it will add 0.

Here is my code for w, be warned I did this in notepad++ so there may be errors.
[code]
Vector2 motion = new Vector2();

int isShiftDown = InputHandler.KeyDown(Keys.LeftShift);
float speedModifier = 2.0f;
float normalSpeed = 1.4f;
if (InputHandler.KeyDown(Keys.W))
{
sprite.CurrentAnimation = AnimationKey.Up;
motion.Y = (float)-(normalSpeed+(isShiftDown*speedModifier));
}

[/code]

Share this post


Link to post
Share on other sites
This code will allow you to walk faster diagonally than you do going straight. If you're not okay with that, here's how to fix it:

[code]
Vector2 motion;
motion.X = InputHandler.KeyDown(Keys.A) ? -1.0f : (InputHandler.KeyDown(Keys.D) ? 1.0f : 0.0f);
motion.Y = InputHandler.KeyDown(Keys.S) ? -1.0f : (InputHandler.KeyDown(Keys.W) ? 1.0f : 0.0f);
motion.Normalize();
motion *= InputHandler.KeyDown(Keys.LeftShift) ? 1.4f : 0.8f;
[/code]
btw, I really like the conditional operator. [img]http://public.gamedev.net/public/style_emoticons/default/tongue.gif[/img]

Share this post


Link to post
Share on other sites
What if I wanted my player to walk diagonally?

[code]
if (//Codition)
{
sprite.CurrentAnimation = AnimationKey.DiagonalUpRight;
//Motion code
}
[/code]

Is it possible to move on both the x and y simultaneously?

Share this post


Link to post
Share on other sites
[quote name='Key' timestamp='1323747901' post='4893393']
What if I wanted my player to walk diagonally?

[code]
if (//Codition)
{
sprite.CurrentAnimation = AnimationKey.DiagonalUpRight;
//Motion code
}
[/code]

Is it possible to move on both the x and y simultaneously?
[/quote]

et1337's solution does make you want diagonally. First, it creates a 2D vector with direction and magnitude, normalizes it so the vector is only contains the direction, then multiplies by the speed as a scalar, making the final vector contain the (deltaX, deltaY) values you want. Maybe you aren't used to conditional operators used so blissfully.

[code]
Vector2 motion = new Vector2();
if (InputHandler.KeyDown(Keys.A))
{
motion.X = -1.0f; // Moving left this frame
}
else if (InputHandler.KeyDown(Keys.D))
{
motion.X = 1.0f; // Moving right this frame
}
if (InputHandler.KeyDown(Keys.S))
{
motion.Y = -1.0f; // Moving down this frame
}
else if (InputHandler.KeyDown(Keys.W))
{
motion.Y = 1.0f; // Moving up this frame
}
motion.Normalize(); // This vector now contains the direction of movement
int speed = 0.8f;
if (InputHandler.KeyDown(Keys.LeftShift))
{
speed = 1.4f;
}
motion = motion * speed; // This vector now contains [deltaX, deltaY]
[/code]

Share this post


Link to post
Share on other sites
Sign in to follow this