Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hi i'm working on my first XNA 2D game and I have a little problem. If I jump, my sprite jumps but does not fall down. And I also have another problem, the user can hold spacebar to jump as high as he wants and I don't know how to keep him from doing that. Here's my code: The Jump :

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

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

The really simple basic Gravity:

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

Here's where's the grounded is set to True or False with a 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;
        }

The grounded bool and The gravity have been tested and are working. Any ideas why? Thanks in advance and don't hesitate to ask if you need another Part of the Code.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

So, I'm not very good at XNA, but if I had to guess, the following would be your issue (unless I'm misunderstanding your code):

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

You are changing the position, even if Jumping should be false again. You'll want to check if Jumping is false after you've checked if they should still be jumping, and then apply the vector. You'll probably also want another variable to see if they have already jumped as high as you allow. Let's call it Falling. You could also do it a single if statement. Something like:

if (FaKeyboard.IsKeyDown(Keys.Space) &&
        (Grounded || (!Falling && xPosition.Y < 10)))
{
    Jumping = true;
}
if (xPosition.Y >= 10) {
    Jumping = false;
    Falling = true;
}

And then, when you are Grounded, you'll want to set Falling = false again.

At least, that's my hunch.

share|improve this answer
    
How could I make this check? –  PhaDaPhunk Dec 1 '11 at 4:40
    
@PhaDaPhunk Updated my answer. Hopefully that helps :) –  Ktash Dec 1 '11 at 4:47
    
Thanks a lot :) –  PhaDaPhunk Dec 1 '11 at 4:48
    
One last little thing, in your example how would you define Falling? –  PhaDaPhunk Dec 1 '11 at 5:27
    
ooh, my C# is rusty, but if I remember correctly, it would be bool Falling = false;. The same way you do Jumping basically –  Ktash Dec 1 '11 at 6:02

You need to change this:

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

To this:

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

Woops, nevermind. Got ninja'd.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.