Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:50:11+00:00 2026-06-02T22:50:11+00:00

I’ve recently started developing with XNA. I could say that I have more that

  • 0

I’ve recently started developing with XNA. I could say that I have more that basic programming language, but nothing so special.

As my first project with XNA. I would like to create a simple 2D platformer, but at the moment, I’m experimenting with simple movement.

I have 3 vectors (position, velocity, acceleration) and a simple integer (maximum velocity):

Vector2 pos = new Vector2(200, 200);
Vector2 vel = new Vector2(20, 20);
Vector2 acc = new Vector2(30, 30);
int maxVel = 50;

And then, I have some simple code that gets in the Update method of the XNA framework to make my sprite move:

// Horizontal Movement
if (KeybState.IsKeyDown(Keys.Right))
{
if (vel.X < maxVel)
vel.X += acc.X * curTime;
pos.X += vel.X * curTime;
}
if (!KeybState.IsKeyDown(Keys.Right) && vel.X > 20)
{
pos.X += vel.X * curTime;
vel.X -= 1;
}

So, when I press the Right arrow key, the sprite is starting to move. The velocity goes from 20, and with the help of acceleration and the elapsed game time, grows to maxVel (which is 50), then when the Right key is released, it loses velocity. It works perfectly.

But when I try to do the same thing for the Left Arrow key, I’m doing pos -= instead of pos+=, which decreases the pos.X value, but it uses a constant velocity (20) instead of increasing it till 50.

I think I’m not smart enough to think about why is this happening, so some help from better developers would be really appreciated.

Note that I do not want to look anywhere else, I want to make my version work as intended, so a straight answer is the best.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:50:19+00:00Added an answer on June 2, 2026 at 10:50 pm

    Your condition

    if (!KeybState.IsKeyDown(Keys.Right) && vel.X > 20)
    

    vill be true every time the right arrow is not pressed (including when the left arrow is pressed) and the speed is higher than 20, so your code promptly decreases the speed back to 20

    You will need to check for whether no key (or at least no arrow key) is pressed to decrease the speed. The simplest way would be:

    if (!KeybState.IsKeyDown(Keys.Right) &&
        !KeybState.IsKeyDown(Keys.Left) &&
        !KeybState.IsKeyDown(Keys.Up) &&
        !KeybState.IsKeyDown(Keys.Down) && vel.X > 20)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.