Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,

I am creating a game in XNA C#, and I am currently creating the jump function. But for some reason it won't work?

My Jump() is triggered when pressed W. And my Gravity() is triggered about 55 times a second.

C#
/* Is the player jumping? */
public bool Jumping = true;

public void Jump()
{
    if (Jumping == false)
    {
        Jumping = true;

        for (int i = 0; i < 8; i++)
        {
            if (!Colliding(0, i))
            {
                this.PositionPixelY -= i;
                CalculatePositions();
            }
        }
    }
}

public void Gravity()
{
    if (!Colliding(0, MovingSpeed) && !Jumping)
    {
        this.PositionPixelY += MovingSpeed;
        CalculatePositions();
    }
    if(Colliding(0, MovingSpeed))
    {
       Jumping = false;
    }
}


Edit: Also, what happens is, when I jump once my character gets stuck with no gravity pulling it down. Sorry I always forget to say what goes wrong.
Posted
Updated 14-Jun-13 0:24am
v2
Comments
Deviant Sapphire 14-Jun-13 6:27am    
Well one of the causes is that I did not write:

for (int i = 0; i < 8; i++)
{
if (!Colliding(0, -i))
{
this.PositionPixelY -= i;
CalculatePositions();
}
}

1 solution

Okay the problem I was having is that it won't go down once we jumped.
This is normal because I am only telling it to go down once we're no longer jumping.
However, we won't be jumping once we reached the ground. A simple solution would be adding a second boolean like so:

C#
public void Jump()
{
    if (Jumping == false)
    {
        GravityBool = false;
        Jumping = true;

        for (int i = 0; i < 8; i++)
        {
            if (!Colliding(0, -i))
            {
                this.PositionPixelY -= i;
                CalculatePositions();
            }
        }

        GravityBool = true;
    }
}

public void Gravity()
{
    if (!Colliding(0, MovingSpeed) && GravityBool)
    {
        this.PositionPixelY += MovingSpeed;
        CalculatePositions();
    }
    else
    {
        Jumping = false;
    }
}



Thanks Codeproject once again for letting my mind having a bit of peace by uploading this problem, and I sorted it out a bit later.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900