Jump to content

  • Log In with Google      Sign In   
  • Create Account

Help with Walk/Sprint Code


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Key   Members   -  Reputation: 112

Like
0Likes
Like

Posted 04 December 2011 - 06:04 PM

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?

        	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;
        	}


Sponsor:

#2 CRYP7IK   Members   -  Reputation: 1013

Like
1Likes
Like

Posted 04 December 2011 - 07:36 PM

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?


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.
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));
}


To accomplish great things we must first dream, then visualize, then plan...believe... act! - Alfred A. Montapert
Gold Coast Studio Manager, Lead Programmer and IT Admin at Valhalla Studios Bifrost.

#3 et1337   Members   -  Reputation: 1704

Like
1Likes
Like

Posted 04 December 2011 - 07:52 PM

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:

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;
btw, I really like the conditional operator. Posted Image

#4 Key   Members   -  Reputation: 112

Like
0Likes
Like

Posted 12 December 2011 - 09:45 PM

What if I wanted my player to walk diagonally?

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

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

#5 compscialien   Members   -  Reputation: 104

Like
0Likes
Like

Posted 13 December 2011 - 09:57 AM

What if I wanted my player to walk diagonally?

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

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


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.

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]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS