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 8973029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:21:27+00:00 2026-06-15T18:21:27+00:00

I want that my character makes a 40 pixel height jump if I press

  • 0

I want that my character makes a 40 pixel height jump if I press the A button once. But I don’t know if 40f is equal to 40 pixel?
What do you think about my code? What is wrong?
In addition, I want that my program runs with the same speed on every computer.

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D image, water;
float Gravity = 5.0F;
float Acceleration = 20.0F;
Vector2 Position = new Vector2(1200,720);
Vector2 Velocity;
float rotation = 0;
SpriteEffects flip;
Vector2 Speed = new Vector2(0, 0);

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    graphics.PreferredBackBufferWidth = 1280;
    graphics.PreferredBackBufferHeight = 720;
}

protected override void Initialize()
{
    base.Initialize();
}

protected override void LoadContent()
{   
    spriteBatch = new SpriteBatch(GraphicsDevice);
    image = Content.Load<Texture2D>("cartoondolphin");
    water = Content.Load<Texture2D>("background");
    flip = SpriteEffects.None;
}

protected override void Update(GameTime gameTime)
{
    float VelocityX = 0f;
    float VelocityY = 0f;

    float time = (float)gameTime.ElapsedGameTime.TotalSeconds;
    KeyboardState kbState = Keyboard.GetState();
    if(kbState.IsKeyDown(Keys.Left)) 
    {
        rotation = 0;
        flip = SpriteEffects.None;
        VelocityX += -5f;
    } 

    if(kbState.IsKeyDown(Keys.Right)) 
    {
        rotation = 0;
        flip = SpriteEffects.FlipHorizontally;
        VelocityX += 5f;
    } 

    // jump if the dolphin is under water
    if(Position.Y >= 670)
    {
        if (kbState.IsKeyDown(Keys.A))
        {     
            if (flip == SpriteEffects.None)
            { 
              VelocityY += 40f;
            }
            else
            { 
              VelocityY += 40f;
            }
        }
    } 
    else 
    {
        VelocityY += -10f;
    }

    float deltaY = 0;
    float deltaX = 0;

    deltaY = Gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;

    deltaX += VelocityX * (float)gameTime.ElapsedGameTime.TotalSeconds * Acceleration;
    deltaY += -VelocityY * (float)gameTime.ElapsedGameTime.TotalSeconds * Acceleration;

    Speed = new Vector2(Speed.X + deltaX, Speed.Y + deltaY);

    Position += Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

    Velocity.X = 0;

    if (Position.Y + image.Height/2 > graphics.PreferredBackBufferHeight)
        Position.Y = graphics.PreferredBackBufferHeight - image.Height/2;

    base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    spriteBatch.Begin();
    spriteBatch.Draw(water, new Rectangle(0, graphics.PreferredBackBufferHeight -100, graphics.PreferredBackBufferWidth, 100), Color.White);
    spriteBatch.Draw(image, Position, null, Color.White, MathHelper.ToRadians(rotation), new Vector2(image.Width / 2, image.Height / 2), 1, flip, 1);
    spriteBatch.End();           

    base.Draw(gameTime);
}
}
  • 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-15T18:21:29+00:00Added an answer on June 15, 2026 at 6:21 pm

    The problem is that you’re not taking the gameTime into account. If a computer with a fast CPU executes that code twice as fast as a slower PC, the value of velocity will increase twice as fast (relative to the slower PC).

    When you have code like:

    VelocityX += 5f;
    

    You need to keep track of the last gameTime, and get the difference between the frames, and multiply that by your constant. (Some code grabbed from Xna adding gravity to a 2d sprite)

    int updateTime = gt.ElapsedGameTime.TotalMilliseconds - oldgt.ElapsedGameTime.TotalMilliseconds;
    
    float timeScalar = updateTime / AVG_FRAME_TIME;
    
    VelocityX += 5f * timeScalar;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a simple task that I want to acheive but ASP.NET makes it
I want to make sure that I only print maximum 80 character long lines,
i want that during marshelling special character should escape, is there any way to
Let’s say that my string is: test! I want to get the first character
I want to match all lines that have any uppercase characters in them but
I want to check that Java String or character array is not just made
[This is on an iSeries/DB2 database if that makes any difference] I want to
If you have a string with special characters that you want to match with:
I have an asp:TextBox and I want to validate that the number of characters
In my regex, I want to say that within the sample text, any characters

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.