Jumping versus. The force of gravity

I am working on my first XNA 2D game and I have a little problem. If I jump, my sprite jumps, but does not fall. And I also have one more problem: the user can hold the space bar to jump as high as he wants, and I don’t know how to keep him from doing this. Here is my code: Go to:

    if (FaKeyboard.IsKeyDown(Keys.Space))
        {
            Jumping = true;
            xPosition -= new Vector2(0, 5);
        }

        if (xPosition.Y >= 10)
        {
            Jumping = false;
            Grounded = false;
        }

Really simple basic gravity:

    if (!Grounded && !Jumping)
        {
            xPosition += new Vector2(1, 3) * speed;
        }

Here, where grounding is set to True or False with Collision

    Rectangle MegamanRectangle = new Rectangle((int)xPosition.X, (int)xPosition.Y, FrameSizeDraw.X, FrameSizeDraw.Y);
        Rectangle Block1Rectangle = new Rectangle((int)0, (int)73, Block1.Width, Block1.Height);
        Rectangle Block2Rectangle = new Rectangle((int)500, (int)73, Block2.Width, Block2.Height);

        if ((MegamanRectangle.Intersects(Block1Rectangle) || (MegamanRectangle.Intersects(Block2Rectangle))))
        {
            Grounded = true;
        }
        else
        {
            Grounded = false;
        }

Grounded bool and The gravity have been tested and work. Any ideas why? Thanks in advance and feel free to ask if you need another piece of code.

+5
source share
4 answers

- / .

:

foreach (Object obj in GameWorld)
{
    obj.Velocity *= 0.5; // Slow it down slightly
    obj.Velocity += GravityPerSecond * FrameTime; // Gravity adjusted for time
    obj.Move(obj.Velocity); // Handle collision and movement here
}

, , , .

:

OnKeyDown(Key k)
{
    if (k == Key.Space)
    {
        obj.Velocity += Vector2(0, 10); // Add an upward impulse
    }
}

, , . (, , ). , , .

, ( ..). , IsJumping .

, , , /, . , ( ).

+5

, :

    if (FaKeyboard.IsKeyDown(Keys.Space))
    {
        Jumping = true;
        xPosition -= new Vector2(0, 5);
    }

    if (xPosition.Y >= 10)
    {
        Jumping = false;
        Grounded = false;
    }

    if (!Jumping && FaKeyboard.IsKeyDown(Keys.Space))
    {
        Jumping = true;
        Grounded = false;
    }

    if (Jumping)
    {
        xPosition -= new Vector2(0, 5);
    } 

    if (xPosition.Y >= 10)
    {
        Jumping = false;
    }

, , , , .

+2

:

const float SCALE = 20.0f; //SPEED!!!
if (jump <= 15 && jump != 0)
            {
                humanPosition.Y -= SCALE * speed;
                jump++;
            }
            else if (jump > 15)
            {
                humanPosition.Y += SCALE * speed;
                jump++;
                if (jump == 32)
                {
                    jump = 0;
                    humanPosition.Y = 307;
                }
            }

            else if (keyboard.IsKeyDown(Keys.Up))
        {
            jump = 1;
            humanPosition.Y -= SCALE * speed;
        }

, , 1 , , , ,

if (humanPosition.Y != GROUNDLEVEL){}
+1

Jumping doesn't have to be logical unless you need it for something like an animation or ability that can only be used while jumping.

Instead, you should use a vector motion, and when a player touches the ground and presses the transition button, add an upward transition vector to your movement.

Then gravity should be directed downward against the player, and it will process itself.

0
source

All Articles