I am making a 2D game where you can jump and shoot and so on. I made a function in my "Player" class called "Jump" and it looks like this:
The code works well as long as you hold down the button. But since it only calls the function when
the jumping only works when the button is pressed. How do I call the function once, and then not call it until the player lands?
public void Jump(Player player1)
{
if (setJump == false)
{
startY = (int)player1.position.Y;
setJump = true;
}
else if (setJump == true)
{
if (canJump == false)
{
player1.position.Y += jumpspeed;//Making it go up
jumpspeed += 0.5f;//Some math (explained later)
if (player1.position.Y >= startY)
//If it's farther than ground
{
player1.position.Y = startY;//Then set it on
canJump = true;
}
}
else
{
canJump = false;
jumpspeed = -15;//Give it upward thrust
setJump = false;
}
}
}
The code works well as long as you hold down the button. But since it only calls the function when
if (keyboard.IsKeyDown(Keys.Space))
{
player1.Jump(player1);
}
the jumping only works when the button is pressed. How do I call the function once, and then not call it until the player lands?