15,877,967 members
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
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();
}
}

## Solution 1

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.