Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

2D jumping sprite problem

$
0
0
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:

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?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>